Logo Linux Bash SSH Ubuntu Git Menu
 

Batch Script With SQLCMD Usage

Tags:

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
like image 603
user52128 Avatar asked Mar 28 '26 12:03

user52128


2 Answers

You need two percent signs in your 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

)

You could also put the "IF NOT" statement just after the sqlcmd, if you wanted to check for an error after each sql command.

like image 122
jftuga Avatar answered Mar 31 '26 17:03

jftuga


Try this line:

FOR %%F IN (C:\SQLCMD\*.SQL) DO sqlcmd -S LENOVO-C00 -U yam -P yam!@ -i %%F -o C:\SEL.txt -p -b

or even better, start debugging with

FOR %%F IN (C:\SQLCMD\*.SQL) DO echo %%F 

to see if this loop works, or if there is a problem with the sqlcmd.

like image 22
Sven Avatar answered Mar 31 '26 16:03

Sven