using sqlcmd in batch file - batch-file

I've scoured StackOverflow for a few hours, and tried different suggestions for similarly asked questions, but nothing passed the parameters correctly so far (double quotes, ^).
Here's the short version of it:
#echo off
cd C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\
start /w "sqlcmd" sqlcmd.exe -S DBserverName -U username -P p#ssword -i C:\query.sql -s"," | findstr /V /C:"-" /B >c:\output.csv
Basically, I want to pass a rather long parameter containing delimiters. But the only way I see that happen is to use arguments. I'd like to keep it as simple as possible. Is this the only recourse? Can you offer an example how this might work?
Thanks in advance.

I'm not sure if it matters, but I think there should be a space between -s and ","
But more importantly, your pipe construct is wrong. Your sqlcmd command is running in a new window, but your pipe is looking for output from the START command itself in the original window - and there isn't any.
You could get your command to work by escaping the pipe and redirection.
#echo off
cd C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\
start /w "sqlcmd" sqlcmd.exe -S DBserverName -U username -P p#ssword -i "C:\query.sql" -s "," ^| findstr /V /C:"-" /B ^>"c:\output.csv"
But there is no need to use START at all. Your script can simply execute sqlcmd directly, and everything is much simpler.
#echo off
cd C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\
sqlcmd.exe -S DBserverName -U username -P p#ssword -i "C:\query.sql" -s "," | findstr /V /C:"-" /B >"c:\output.csv"
You might also be running into problems with your password, depending on what characters are used. You might have to quote and/or escape the password, and sqlcmd.exe might have its own escape rules. If it does, then you might have to worry about escaping for both cmd.exe and sqlcmd.exe.

This line should have double quotes around the entire path, in some cases it matters.
cd C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\
and the start command needs double quotes at the beginning because it takes the first set as the window title.
start "" /w ....

Related

SQLCMD command, How to save output into log file

The following question has helped me solving the problem of executing multiple SQL Scripts located in file. Run all SQL files in a directory
However, I did not get how to redirect the output into a separate log file. Someone suggested the following script but since I don't understand it, it did not work and I can't find out the error.
for %f in (*.sql) do sqlcmd /S <servername> /d <dbname> /E /i "%f" >> sql.log 2>&1)
If you need the output into one common file then you should use the #Abhishek 's answer.
If you need the output into a separate log file for an each input sql file
then you can use -o parameter of sqlcmd command. Your bat file could look like this:
for %%G in (*.sql) do sqlcmd /S <servername> /d <dbname> -E -i"%%G" -o C:\logs\%%G.log
pause
In this case for
1.sql
2.sql
you will get:
1.sql.log
2.sql.log
You are seeking Command Redirection.
As per your example -
for %f in (*.sql) do sqlcmd /S <servername> /d <dbname> /E /i "%f" >> sql.log 2>&1
once the execution of the sql script is done the output will be redirected to and appends the command output to the end of file (here sql.log) without deleting the information that is already in the file (>>) and redirects STDERR (2) into STDOUT handle(1) - 2>&1
More information here and here.

Run all SQL files in multiple directories

I combined a couple of solutions I found online to try and make this happen.
https://stackoverflow.com/a/6504317/2471473
https://sqlandme.com/2013/03/25/sql-server-executing-multiple-script-files-using-sqlcmd/
I'm trying to run a single .cmd script (Script1.cmd) with folder locations of .sql files. That single script runs another script (Script2.cmd) to use sqlcmd to execute all the .sql files in that folder location. It mostly works, but it leaves a command window open that I have to exit from for each folder location.
Script1.cmd
start Script2.cmd "C:\Location1"
start Script2.cmd "C:\Location2"
Script2.cmd
#Echo Off
FOR /f %%i IN ('DIR %1\*.Sql /B') do call :RunScript %1 %%i
GOTO :END
:RunScript
Echo Executing Script: %2
cd %1
SQLCMD -S Server123 -d Database456 -E -i %2
Echo Completed Script: %2
:END
Official command line reference for Windows XP or for Windows Server 2003, Windows Vista (and above) seems to be too brief. Read this (extended) start command documentation:
Syntax: START "title" [/D path] [options] "command" [parameters]
Always include a TITLE this can be a simple string like "My Script" or
just a pair of empty quotes "". According to the Microsoft
documentation, the title is optional, but depending on the other
options chosen you can have problems if it is omitted.
If command is an internal cmd command or a batch file then the command
processor is run with the /K switch to cmd.exe. This means that the
window will remain after the command has been run.
Next Script1.cmd should work and close started command windows:
start "" cmd /C Script2.cmd "C:\Location1"
start "" cmd /C Script2.cmd "C:\Location2"

Batch script escape space not working

When I try to run the command
C:\Windows\system32\cmd.exe /C "C:\Program Files (x86)\TorAES\certmgr.exe" -del -c -n "Certificatename" -s -r localMachine trustedpublisher
in a Windows cmd.exe I get the errormessage (translated by me):
The command "C:\Program" is misspelled or does not exist.
even other combinations with " does not work:
C:\Windows\system32\cmd.exe /C 'C:\Program Files (x86)\TorAES\certmgr.exe' -del -c -n 'Certificatename' -s -r localMachine trustedpublisher
C:\Windows\system32\cmd.exe /C "'C:\Program Files (x86)\TorAES\certmgr.exe' -del -c -n 'Certificatename' -s -r localMachine trustedpublisher"
C:\Windows\system32\cmd.exe /C \"C:\Program Files (x86)\TorAES\certmgr.exe\" -del -c -n \"Certificatename\" -s -r localMachine trustedpublisher
Unfortunately if I just run
C:\Windows\system32\cmd.exe /C "C:\Program Files (x86)\TorAES\certmgr.exe"
certmgr.exe starts but (obvious) my arguments are missing and i really need them to automatic remove my certificate when uninstalling my program.
I am absolutly unfamiliar with batchscripting.
Is someone able to find my mistake?
Thanks!
See cmd /?:
If /C or /K is specified, then the remainder of the command line after the switch is processed as a command line, where the following logic is used to process quote (") characters:
If all of the following conditions are met, then quote characters on the command line are preserved:
no /S switch
exactly two quote characters
no special characters between the two quote characters,
where special is one of: &<>()#^|
there are one or more whitespace characters between the
two quote characters
the string between the two quote characters is the name
of an executable file.
Otherwise, old behavior is to see if the first character is
a quote character and if so, strip the leading character and
remove the last quote character on the command line, preserving
any text after the last quote character.
So you can add an outer set of quotes:
C:\Windows\system32\cmd.exe /C ""C:\Program Files (x86)\TorAES\certmgr.exe" -del -c -n "Certificatename" -s -r localMachine trustedpublisher"

how to execute all .sql file of current and sub folder using xp_cmdshell

I am using sql server 2008 , I am developing script that get all .sql file of given path ( also serch in subfolder recursively). Thanks in advance.
You could use a batch file like this. Call it ashwin.bat (or whatever you like) and it will look for all the files in C:\tmp\so\ashwin that have a .sql extension and then invokes sqlcmd against all of those files against a named instance database of localhost\localsqla and runs them in the master database.
#echo off
For /R "C:\tmp\so\ashwin\" %%i in (*.sql) DO CALL sqlcmd.exe -E -S localhost\localSQLA -d master -i %%i
A litle enhancement for logging purposes:
#echo off
For /R "C:\Deploy\SQL" %%i in (*.sql) DO CALL echo %%i && sqlcmd.exe -E -S DB_IP -d DATABASE -i %%i -j

How to tell forfiles to execute command in PATH?

I'm missing something (obvious?) about escaping my strings or spaces in the following Windows Server 2k3 batch command.
FORFILES -m *.wsp -c "CMD /C C:\Program^ Files\Common^ Files\Microsoft^ Shared\web^ server^ extensions\12\bin\stsadm.exe^ -o^ addsolution^ -filename^ #FILE"
Results in the following error
'C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\bin\stsadm.exe -o addsolution -filename "foobar.wsp"' is not recognized as an internal or external command,operable program or batch file.
But I can't figure out why. I'm working off Mr. Simon Sheppard's fine documentation
The path needs to be quoted, and the quote must be escaped.
FORFILES -m *.wsp -c "CMD /C ^0x22C:\Program^ Files\Common^ Files\Microsoft^ Shared\web^ server^ extensions\12\bin\stsadm.exe^0x22 -o^ addsolution^ -filename^ #FILE"
A co-worker suggested using the hex for ", and I eventually figured out that the hex needed escaping.
Another possible answer is to use the old 8.3 names you get by doing dir /X.
Like: C:\PROGRA~1 instead of C:\Program Files.

Resources