批处理文件&的错误处理Sqlcmd [英] Error Handling with Batch File & Sqlcmd

查看:68
本文介绍了批处理文件&的错误处理Sqlcmd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件,该批处理文件使用sqlcmd运行一些SELECT查询,将结果放入文本文件中,然后将这些文件上传到FTP服务器上.一切都按应有的方式进行,这就是我希望工作的方式.

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 should, which is how I like things to work.

我一直想知道如果发生错误我该怎么办.假设有人更改了我访问的数据库的数据结构,但没有通知我.如果我运行sqlcmd SELECT语句并将结果放入文本文件中,则最终将得到一个包含错误的文本文件,该文件将直接进入FTP,就好像什么都没错一样.(我已经对此进行了测试.)

I've been wondering about what I would do in the event of an error, though. Let's say someone changes the data structure of the database I'm hitting and doesn't notify me. If I ran a sqlcmd SELECT statement and dropped the result into a text file, I would just end up with a text file containing an error, which would then go straight to the FTP as if nothing was wrong. (I've tested this.)

我希望能够检查来自sqlcmd的错误-超时,凭据错误,查询格式错误等,我不确定这样做是如何进行的还是最佳实践"是什么.我总是可以尝试抓取输出的文本文件并搜索我认为可能会发生的错误,但是由于许多原因,这是有问题的.

I would like to be able to check for errors coming from sqlcmd--timeouts, bad credentials, malformed query, etc etc, I'm just not sure how this is done or what the "best practice" is. I could always try to crawl the output text file and search for errors I think might happen, but this is problematic for any number of reasons.

任何人都愿意分享这些经验吗?

Anyone have any experience with this that they'd care to share?

推荐答案

您可以检查从 SQLCMD 返回的错误级别,以查看是否失败.

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

这篇关于批处理文件&amp;的错误处理Sqlcmd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆