Stopping executing of SQL scripts after error occurs - sql

I am executing multiple SQL scripts using batch file but now I want to stop execution of batch file if error occurs in any one of the scripts.
This is my batch file:
sqlcmd -S dbdev026b\dbdev026b -i c:Test1.sql -o c:\o1.txt
sqlcmd -S dbdev026b\dbdev026b -i c:Test2.sql -o c:\o2.txt

sqlcmd -S dbdev026b\dbdev026b -i c:Test1.sql -o c:\o1.txt || exit /b 1
sqlcmd -S dbdev026b\dbdev026b -i c:Test2.sql -o c:\o2.txt || exit /b 2

Related

Unexpected argument executing cmdexec on a SQL job to export to CSV

I try to run this on a SQL job:
sqlcmd -S . -d CI_Reports -E -s"," -W -Q "SET NOCOUNT ON SELECT * FROM [dbo].[Table]" > D:\Test.csv
How can I fix this error?
Sqlcmd: '> D:\Test.csv': Unexpected argument.
Have you tried like this -
sqlcmd -S . -d CI_Reports -E -s"," -W -Q "SET NOCOUNT ON SELECT * FROM [dbo].[Table]" -o D:\Test.csv
where -o output_file which would identify the file that receives output from sqlcmd.
Additionally you could try BCP which is best suited for bulk coping data between an instance of Microsoft SQL Server and a data file in a user-specified format.
Read more here.

Stored procedure execution

I have written a stored procedure to be called from sqlcmd (batch file). This is the code - for some reason it is not executing.
#ECHO OFF
SET /P FDate=Enter From Date:
SET /P TDate=Enter To Date:
ECHO sqlcmd -E -Q "dbo.SavingsAccountsAllDetail #FDate=N'%Param1%', #TDate=N'%Param2%'" -S OMNIDB-UAT -d HNBG_LOAN_TEST -o C:\SavingsAccountsAllDetailRepo.txt
SET Param1=
SET Param2=
Any thoughts?
Try to add EXEC before procedure's name:
sqlcmd -E -Q "exec dbo.SavingsAccountsAllDetail #FDate=N'%Param1%', #TDate=N'%Param2%'" -S OMNIDB-UAT -d HNBG_LOAN_TEST -o C:\SavingsAccountsAllDetailRepo.txt
You can also try to passparameters via the /v argument. More datais here
sqlcmd -E -Q "exec dbo.SavingsAccountsAllDetail #FDate=N'$(Param1)', #TDate=N'$(Param2)'" /v Param1=%Param1% Param2=%Param2% -S OMNIDB-UAT -d HNBG_LOAN_TEST -o C:\SavingsAccountsAllDetailRepo.txt

Exclude files that start with number - batch script

How to exclude the files from the loop -> if the file name in a directory starts with number.
for %%G in (*.sql) do sqlcmd -S %SName% -U %UName% -P %Pwd% -d %DbName% -Q "EXECUTE sp_deploy %%~nG" >> "C:\%%~nG.sql" 2>&1
I do not want to execute the sql if the file name start with a number. How can I stop that..
Thanks in advance.
"if '%%~nG' not like '[0-9]%' EXECUTE sp_deploy %%~nG"

DOS batch file - loop and increment by 1

I have this batch file that logs into sql on a remote machine runs a stored procedure and then sends the output to a text file. I would like it to increment both the 3rd octet in the IP address and the name of the output text file by 1 and loop so I don't have to repeat the command over and over. Also, I would like it to stop when it reaches a certain number. Is there a way to do this?
sqlcmd -U user -P password -S 192.168.1.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput01.txt
sqlcmd -U user -P password -S 192.168.2.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput02.txt
sqlcmd -U user -P password -S 192.168.3.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput03.txt
sqlcmd -U user -P password -S 192.168.4.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput04.txt
sqlcmd -U user -P password -S 192.168.5.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput05.txt
this will do what you want. the /L specifier tells it to act like a regular programming for loop. the first parameter is the starting integer, the next is the number to step, and the last is the count. so this will loop starting at 1, incrementing by 1 for 6 integers:
#echo off
FOR /L %%G IN (1, 1, 6) DO (
echo sqlcmd -U user -P password -S 192.168.%%G.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput0%%G.txt
)
and if you want to do a list of IPs instead of a range, you can leave off the /L and just list the numbers. e.g.
FOR %%G IN (1,2,3,99,121) DO ...
obviously the "echo" before sqlcmd is just for testing;)
On the assumption that you're actually using cmd.exe rather than MS-DOS, one way to increment and test a variable is as follows:
#setlocal enableextensions enabledelayedexpansion
#echo off
set /a "i = 1"
:loop
if !i! leq 15 (
if !i! lss 10 (
echo sqlcmd -S 192.168.!i!.2 -o c:\sql\ouput0!i!.txt
) else (
echo sqlcmd -S 192.168.!i!.2 -o c:\sql\ouput!i!.txt
)
set /a "i = i + 1"
goto :loop
)
endlocal
This is a slightly modified version of what you need which echoes the relevant bits rather than executing them, and it outputs:
sqlcmd -S 192.168.1.2 -o c:\sql\ouput01.txt
sqlcmd -S 192.168.2.2 -o c:\sql\ouput02.txt
sqlcmd -S 192.168.3.2 -o c:\sql\ouput03.txt
sqlcmd -S 192.168.4.2 -o c:\sql\ouput04.txt
sqlcmd -S 192.168.5.2 -o c:\sql\ouput05.txt
sqlcmd -S 192.168.6.2 -o c:\sql\ouput06.txt
sqlcmd -S 192.168.7.2 -o c:\sql\ouput07.txt
sqlcmd -S 192.168.8.2 -o c:\sql\ouput08.txt
sqlcmd -S 192.168.9.2 -o c:\sql\ouput09.txt
sqlcmd -S 192.168.10.2 -o c:\sql\ouput10.txt
sqlcmd -S 192.168.11.2 -o c:\sql\ouput11.txt
sqlcmd -S 192.168.12.2 -o c:\sql\ouput12.txt
sqlcmd -S 192.168.13.2 -o c:\sql\ouput13.txt
sqlcmd -S 192.168.14.2 -o c:\sql\ouput14.txt
sqlcmd -S 192.168.15.2 -o c:\sql\ouput15.txt
Simply take the echo off the line and adjust the command to put back the other bits.
If you have access to cmd.exe then you also have access to cscript which allows you to write a DOS batch file in Javascript.

SQLCMD Usage in Batch file

Hi All I am Writing a Batch Script Which has to read a set of SQL Files which exists in a Folder then Execute Them Using SQLCMD utiliy.
When I am Trying to execute it does not create any output file. I am not sure where I am wrong and I am not sure how to debug the script. Can someone help me out with script?
#echo off
FOR %F IN (C:\SQLCMD*.SQL) DO sqlcmd -S LENOVO-C00 -U yam -P yam!# -i %F -o C:\SEL.txt -p -b
IF NOT [%ERRORLEVEL%] ==[0] goto get_Error
:Success echo Finished Succesffuly exit /B 0 goto end
:get_error echo step Failed exit /B 40
:end
If this is actually in a batch file (not being executed by hand at the command line), then the %F variables need to have the % characters doubled-up because of the way that cmd.exe executes the lines read from the batch file:
FOR %%F IN (C:\SQLCMD*.SQL) DO sqlcmd -S LENOVO-C00 -U yam -P yam!# -i %%F -o C:\SEL.txt -p -b
Though I would have thought you'd get a
F was unexpected at this time.
error if you only had one % character in front of the variable name.
Try to right click the Batch File and run as administrator.
Seems like the answer would be more like this:
FOR %%F "usebackq" IN (`dir.exe C:\SQLCMD*.SQL`) DO ...