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,755
  • 5
  • 40
  • 52
Sreedhar
  • 27,817
  • 33
  • 106
  • 176

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,270
  • 9
  • 68
  • 91
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
  • 120,522
  • 13
  • 212
  • 426