Error Handling with Batch File & Sqlcmd

前端 未结 4 1807
傲寒
傲寒 2021-02-07 07:19

I have a batch file that runs some SELECT queries using sqlcmd, puts the results into text files, and uploads those files onto an FTP server. That\'s all working just the way it

相关标签:
4条回答
  • 2021-02-07 07:19

    I would maybe start with putting return values in your SQL, like so:

    DECLARE @IsBored bit = 1
    
    ... do work ...
    
    SELECT 0 -- Success!
    

    You could take it a bit further and use TRY/CATCH blocks to trap errors and return an error code. With SQLCMD, you can use the return code from your SQL as the exit code of the application, like so:

    sqlcmd -b -S ServerName -E -d DbName -q "EXIT(EXEC dbo.YourProc)" 
           -o "C:\Logs\output.log" -u
    

    If you were managing your SQLCMD calls with something like a scheduler, you could take action based on returns codes from SQLCMD. Since you're just using batch files, I think you can do something like this:

    @ECHO OFF
    sqlcmd -b -S ServerName -E -d DbName -q "EXIT(EXEC dbo.YourProc)" 
           -o "C:\Logs\output.log" -u
    IF %ERRORLEVEL% NEQ 0 ECHO "Error"
    

    Good luck!

    0 讨论(0)
  • 2021-02-07 07:21

    You can check errorlevel returned from SQLCMD to see if it failed.

        sqlcmd -b <yourscript>
        IF ERRORLEVEL 1 goto err_handler
        goto done
        :err_handler
        REM handle the error here
    
        :done 
        REM script completion code here
    
    0 讨论(0)
  • 2021-02-07 07:26
    for %%G in (*.sql) do (sqlcmd /S %sqlhost% /d %sqldbname% -E -b -i "%%G" >> output.txt if ERRORLEVEL 1 exit)
    

    Above code will loop through all the *.sql in a folder. If error encountered in any of the script , error will get logged in the output.txt file and it will stop the batch process immediately.

    0 讨论(0)
  • 2021-02-07 07:33

    I built a minilanguage in python to solve a similar problem. Using the subprocess library, you can run your code through sqlcmd, then get the output and any error codes. Parse the output before putting it into your text file, and if necessary go through error repair states. These states can modify the code or options and retry sending them through sqlcmd. If all else fails, email a human.

    Of course, since I was using python like that, I didn't bother with sqlcmd at all and just used the odbc python libraries for a direct connection to the database. I could rollback my transactions if I had a catastrophic failure, run it in interactive mode, or via a command file etc. etc.

    That's a pile of work though. For more simplistic error checking, just add a filter to your pipeline, say grep or awk. Or roll your own with flex.

    0 讨论(0)
提交回复
热议问题