2

I get a bat file as below:

@ECHO Executing scripts...
PAUSE 

for %%X in (*.SQL) do SQLCMD -S localhost -d CTL  -I -i "%%X" >> ResultScript.txt

pause

In this I want to has user inputs for localhost (Sql server instance) and CTL (database). How can this be achieved in DOS (os: WinXP)

Thanks

Dan
  • 5,929
  • 6
  • 42
  • 52
Sreedhar
  • 29,307
  • 34
  • 118
  • 188

2 Answers2

4
SET /P variable=PromptString

So your batch file would be something like this

@ECHO Executing scripts... 
PAUSE  
SET /P sqlServer=Please enter SQLServer: 
SET /P CTL=Please enter CTL:
for %%X in (*.SQL) do SQLCMD -S %sqlServer% -d %CTL%  -I -i "%%X" >> ResultScript.txt 

pause
jball
  • 24,791
  • 9
  • 70
  • 92
1

Use parameter markers, where %1 refers to the first parameter, %2 the second, and so forth.:

for %%X in (*.SQL) do SQLCMD -S %1 -d %2  -I -i "%%X" >> ResultScript.txt

If your batch file was called ExecScript.bat, your user would run it as

ExecScript instancename databasename

You'll probably want to add a test above the for loop to make sure both parameters are passed in. Running SQLCMD with a blank instance and database wouldn't work too well.

Ken White
  • 123,280
  • 14
  • 225
  • 444