views:

30

answers:

2

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
+1  A: 

Try to right click the Batch File and run as administrator.

algorian
+1  A: 

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.

Michael Burr
Hi Michael Thanks for the Reply.After Adding the %% I was able to Run the batch file. But the script was not able to loop or iterate the file's.
Anoop
Anoop - comment out the "@echo off" so you can see exactly what commands the `for` command is trying to execute. That may give you a clue to what's going wrong. Alternatively, change the "DO sqlcmd ..." to "DO echo sqlcmd ..." to maybe help debug what's going on.
Michael Burr