How to make SQLCMD display only errors and warnings

How can you get SQLCMD when executing an SQL script file to just display any errors or warnings it encounters?

I, in fact, do not want to display messages based on information.

+5
source share
3 answers

It is impossible to prevent the SQLCMDreturn of output messages without errors by passing a parameter to it.

However, you can redirect error messages to STDERR, and then forward all other messages to NUL.

This is done by passing a parameter -r. From a book online :

-r [0 | 1] msgs to stderr

(stderr). 0, 11 . 1, , PRINT, . , -o. .

-r , , , :

sqlcmd -Q "select 1 as a; select 1/0 as b" -E -r1 1> NUL
+15

, , https://www.simple-talk.com/sql/sql-tools/the-sqlcmd-workbench/

. OUT, . , :

sqlcmd -x -E -S MyServer -i C:\MySQLBatchToRun.sql 

MySQLBatchToRun.sql -

USE MyDatabase
:Error C:\MyErrorLog.txt
:r C:\MySQLScript.sql
GO

MySQLScript.sql SQL . , . , , , , .

+1

, print stderr -r1, :

sqlcmd -Q "print 'hello logfile';select 'Ted' as bro"  -r1 1> C:\output.txt 2> C:\logfile.txt

-i , :   sqlcmd -i helloTed.sql -r1 1 > C:\output.txt 2 > C:\logfile.txt

helloTed.sql:

'hello logfile',
'Ted' bro

Perhaps you can use -Q and insert an exec stored process that contains fingerprints.

0
source

All Articles