3

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 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

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?
3
$ 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.