Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

echo Finished Succesffuly
exit /B 0
goto end

echo step Failed
exit /B 40

share|improve this question

You need two percent signs in your batch file:


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.

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.