sql-server-2005batch-fileosql

How to get error name in batch when I use OSQL command?


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.


Solution

  • 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