I am executing a bunch of SQL scripts using SQLCMD, and redirecting the output to a log file using the -o switch. How do I turn off the output of SELECT queries? Errors, if any, should still be written to the log file, as usual.
2 Answers
If you want to log only errors, log by redirecting STDERR rather than by using the -o switch:
SQLCMD -S server -d database -E -r0 -Q "select 1" 2> error.log
update
Added that the -r0 switch must be set to redirect errors to STDERR
1 Comment
GPX
Thanks! That did the trick. Also, how do I not display the query output on the console? Is there a way to redirect
STDOUT to null?$ sqlcmd -S server -d database -E -r0 -Q "select 1" 1> NUL 2> NUL
-Q allows you to specify your query.
1> is for the stdout. You can use 1> logs.txt instead to write the output into a file.
2> is for the stderr. You can use 2> errors.txt instead to write the errors into a file.
Further reading is available on sqlcmd reference from Microsoft.