How to get error name in batch when I use OSQL command? - sql-server-2005

I created a batch file which execute a sql command.
Let's see the piece of code
SET OSQL="C:\Program Files (x86)\Microsoft SQL Server\90\Tools\Binn\osql.exe"
IF NOT EXIST %OSQL% ECHO OSQL.EXE not found, please check the OSQL variable!
IF NOT EXIST %OSQL% GOTO :ERROR
SET SQLQUERY_UPDATEDB = "SELECT * FROM %DB%";
%OSQL% -S %SQLSERVERNAME% -d %DBNAME% -U %DBACCOUNT% -P %DBPASSWORD% -Q %SQLQUERY_UPDATEDB% -n -b -m-1 > D:\sqloutput.txt
:ERROR
ECHO Sorry, could not complete your request!
ECHO %ERRORLEVEL%
GOTO :END
:END
ECHO Finish batch
The above part of code returns always 0 and Sorry, could not complete your request! . What does mean ?
How to set up to display error in console to see exactly what does wrong ?
I put -m-1 and I removed -h-1 accordingly with MSDN but doesn't show up in my console.
Thank you.

Um. Because even if it runs the SQL Command successfully, it then drops into the error handler?
Try adding a GOTO :END after you run the command:
SET SQLQUERY_UPDATEDB = "SELECT * FROM %DB%";
%OSQL% -S %SQLSERVERNAME% -d %DBNAME% -U %DBACCOUNT% -P %DBPASSWORD% -Q %SQLQUERY_UPDATEDB% -n -b -m-1 > D:\sqloutput.txt
GOTO :END
:ERROR

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.

Select after login via Batch (Microsoft SQL Server Management Studio)

I am trying to select data from table and save them info .csv file via .bat file.
My batch runs Microsoft SQL Server Management Studio and log me into database, but I am not able to execute sql scripts.
Here is what I have:
#echo off
cls
:begin
set /p SName=Server Name :
set /p DbName=Database Name :
set /p UName=User Name :
set /p DbPWD=Password :
pause
echo Running Microsoft SQL Server Management Studio, please wait
call ssms -S %SName% -d %DbName% -U %UName% -P %DbPWD% "select * from MYTABLE" -s "," -o " Export.csv"
echo Done
pause
:end
It does not work after line: ssms -S %SName% -d %DbName% -U %UName% -P %DbPWD%
UPDATE:
Error picture
Thank you
SSMS does not accept an in-line script from the command line, as you are trying to do. Please use SQLCMD for this work. that's why you got the error message.
See SSMS Command Line
Straight from Microsoft
sqlcmd -S <ComputerName>\<InstanceName> -i <MyScript.sql> -o <MyOutput.rpt>

Stopping executing of SQL scripts after error occurs

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

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"

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 ...