Reading the SQL 2005 instance and installation directory from a DOS batch file - Jon Galloway

Reading the SQL 2005 instance and installation directory from a DOS batch file

SQL 2005 installs each instance to a numbered directory based on the order installed: MSSQL.1, MSSQL.2, etc. That makes scripting a little tricky. Fortunately, you can find them in the registry.

The install path for the SQL 2005 database server is here:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\SQL2005\Setup\SQLPath

You can look up specific instances in the the Instance Names registry node. For instance, to find the SQL2005 instance, you'd look at this key:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL\SQL2005

Looking up registry values in a DOS batch file is a little tricky. Fortunately, Robert VanderWoude has a great tip for reading registry values via creative use of the FOR /F command.

So, with all that out of the way, we can read the SQL Server instance with this line:

FOR /F "tokens=2* delims= " %%A IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL" /v SQL2005') DO SET SQLINSTANCE=%%B

and the SQL Server base installation directory with this:

FOR /F "tokens=2* delims= " %%A IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\SQL2005\Setup" /v SQLPath') DO SET DATADIRECTORY=%%B\Data

Why would you want to read that kind of stuff in a batch file? Well, to write a Batch files to check SQL 2005 (MDF) files in and out of Subversion source control, or course.

Published Saturday, October 28, 2006 10:19 PM by Jon Galloway

Comments

# re: Reading the SQL 2005 instance and installation directory from a DOS batch file

Is there any way to determine an instance through a T-SQL query?

Thursday, May 17, 2007 1:17 PM by Will Hutton

# re: Reading the SQL 2005 instance and installation directory from a DOS batch file

Didn't find this path(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL\SQL2005) in registry to find the sql sever instance.

Monday, April 21, 2008 1:46 AM by baddy

# re: Reading the SQL 2005 instance and installation directory from a DOS batch file

Hello,

Please add your site at http://www.sweebs.com. Sweebs.com is a place where other people can find you among the best sites on the internet!

Its just started and we are collecting the best found on the net! We will be delighted to have you in the sweebs listings.

Regards

Kris

Monday, March 30, 2009 8:25 AM by Kris

# re: Reading the SQL 2005 instance and installation directory from a DOS batch file

Hi there.

Great installation help...thanks, fixed my problem.

Anyone reading this guys stuff should bookmark it.

Tuesday, February 02, 2010 8:30 AM by Install Software

# re: Reading the SQL 2005 instance and installation directory from a DOS batch file

Another great post.

Thanks for the tips and help.

Everyone, bookmark this site.

Friday, February 05, 2010 9:57 AM by Install Software

# re: Reading the SQL 2005 instance and installation directory from a DOS batch file

Greeting. In the absences of a decent time machine, fiction remains the most sturdy vehicle for visiting other eras. Help me! It has to find sites on the: Share trading class chennai. I found only this - <a href="tt.tlu.ee/.../share-trading-on-hdfc">share trading on hdfc</a>. For inclusion, four out of the five most annual merchants in the growth are these four ongoing flexibility unitholders, share trading. Share trading, tsunomon digivolved to gabumon during the margin against the digidestined's last money - kuwagamon. With best wishes :-(, Nibal from Afghanistan.

Tuesday, March 23, 2010 10:18 PM by Nibal

# re: Reading the SQL 2005 instance and installation directory from a DOS batch file

I got the following error

%%A was unexpected at this time

Thursday, May 06, 2010 3:29 AM by Test

# re: Reading the SQL 2005 instance and installation directory from a DOS batch file

Ditto on not finding the key in the registry.  I have Microsoft SQL Server 2005 - 9.00.4035.00

Wednesday, August 18, 2010 9:05 AM by Jean-Pierre Fouche

# re: Reading the SQL 2005 instance and installation directory from a DOS batch file

Please, can you PM me and tell me couple of far more thinks about this, I am seriously lover of your web site...gets solved properly asap."

--------------------------------------------

<a href="xiangyan.info/8mg-p-123.html">&

Also welcome you!

Sunday, November 28, 2010 11:41 AM by &#32445;&#32422;&#30041;&#23398;&#29983;

# re: Reading the SQL 2005 instance and installation directory from a DOS batch file

We must accept finite disappointment, but we must never lose infinite hope.

-----------------------------------

Friday, December 17, 2010 5:04 PM by flip ipad app

# re: Reading the SQL 2005 instance and installation directory from a DOS batch file

-----------------------------------------------------------

"I entirely agree while using over viewpoint, the globe vast web is definitely with out a doubt rising in to your principal kind of conversation close to the planet and  it is due to to web sites like this that concepts are spreading so swiftly."

Tuesday, January 04, 2011 5:38 AM by best ipad covers

# re: Reading the SQL 2005 instance and installation directory from a DOS batch file

-----------------------------------------------------------

"Nice article! GA can be my biggest incomes. On the other hand, it is not a very much."

Sunday, January 09, 2011 6:41 AM by ipad app reviews

# re: Reading the SQL 2005 instance and installation directory from a DOS batch file

So-so. Something was not impressed.

Wednesday, March 28, 2012 10:29 AM by Dylan

Leave a Comment

(required) 
(required) 
(optional)
(required)