Parse a connection string for use in SQLCMD

Microsoft Windows xp professional w/serv...
August 6, 2010 at 09:25:48
Specs: Windows XP
I am trying to write a batch script that makes a call to a database using SQLCMD and connects to the database via a connection string stored in an environment variable.

The difficulty I'm having with this is that you need to explicity pass SQLCMD the server and database information, it can't take a connection string -- so I want to parse out the parameters from the environment variable.

Can anyone show me how to parse the "Data Source" and "Initial Catalog" out of a connection string stored in an environment variable?

Provider=SQLNCLI.1;Data Source=SERVER\INSTANCE;Integrated Security=SSPI;Initial Catalog=DATABASENAME;Connect Timeout=90


See More: Parse a connection string for use in SQLCMD

Report •


#1
August 6, 2010 at 09:44:59
SET _conn=Provider=SQLNCLI.1;Data Source=SERVER\INSTANCE;Integrated Security=SSPI;Initial Catalog=DATABASENAME;Connect Timeout=90
SET _conn=%_conn: =%
SET _conn="%_conn:;=";"%"
FOR %%a IN (%_conn%) DO @FOR /F %%b IN (%%a) DO SET _%%b
SET _conn=
SET _


Report •

#2
August 6, 2010 at 12:23:53
Much nicer than how I was trying to do it. Thanks a bunch.

Report •

Related Solutions


Ask Question