问题描述
如何让 SQLCMD 在执行 SQL 脚本文件时只输出它遇到的任何错误或警告?
How can you get SQLCMD, when executing a SQL script file, to just output any errors or warnings it encounters?
我基本上不希望输出基于信息的消息.
I essentially dont want information based messages to be output.
推荐答案
通过向 SQLCMD
传递参数来阻止它返回非错误输出消息是不可能的.
It's not possible to prevent SQLCMD
returning non-error output messages by passing it a parameter.
但是,您可以做的是将错误消息重定向到 STDERR
,然后将所有其他消息重定向到 NUL
.
However, what you can do is redirect error messages to STDERR
, then direct all other messages to NUL
.
这是通过传递 -r
参数来完成的.来自在线书籍:
This is done by passing the -r
parameter. From books online:
-r[ 0 |1] 向 stderr 发送消息
将错误消息输出重定向到屏幕(stderr).如果不指定参数或指定 0,则仅重定向严重性级别为 11 或更高的错误消息.如果指定 1,则重定向包括 PRINT 在内的所有错误消息输出.如果使用 -o 则无效.默认情况下,消息会发送到标准输出.
Redirects the error message output to the screen (stderr). If you do not specify a parameter or if you specify 0, only error messages that have a severity level of 11 or higher are redirected. If you specify 1, all error message output including PRINT is redirected. Has no effect if you use -o. By default, messages are sent to stdout.
设置 -r
取决于您要显示的错误消息,但要显示所有错误消息输出,示例命令为:
Set -r
depending on exactly which error messages you want to display, but to display all error message output, an example command would be:
sqlcmd -Q "select 1 as a; select 1/0 as b" -E -r1 1> NUL
这篇关于如何让 SQLCMD 只输出错误和警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!