如何让 SQLCMD 仅输出错误和警告

2022-01-16 00:00:00 sql sql-server sql-server-2008-r2 sqlcmd

如何让 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] 向标准错误发送消息

-r[ 0 | 1] msgs to 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

相关文章