Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get SQLCMD to output errors and warnings only

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.

like image 404
bytedev Avatar asked Jun 28 '12 11:06

bytedev


People also ask

How do I turn off hyphens in SQLCMD?

use the -h -1 option to remove the dashes (--------) from the output and SET NOCOUNT ON to remove the "rows affected". This is great if you're creating a report or CSV file for another system to process.

How can I tell if SQLCMD is working?

You could try to execute sqlcmd.exe -? in a process in your C# app - if it works, then SQLCMD is present - if not, it'll tell you something like "file not found" or "command invalid" or something ....

What is the purpose of using SQLCMD Q command?

You can use sqlcmd to execute database script files. Script files are text files that contain a mix of Transact-SQL statements, sqlcmd commands, and scripting variables.

How do I use Windows authentication in SQLCMD?

Connect to a SQL Server instance using Windows Authentication. Open the Command Prompt and switch to the location of the sqlcmd utility. Then, execute the following command by replacing the connection parameters with the server ( server_name ) and instance ( instance_name ) names to which you want to connect.


2 Answers

It's not possible to prevent SQLCMD returning non-error output messages by passing it a parameter.

However, what you can do is redirect error messages to STDERR, then direct all other messages to NUL.

This is done by passing the -r parameter. From books online:

-r[ 0 | 1] msgs to stderr

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.

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
like image 94
Ed Harper Avatar answered Sep 17 '22 15:09

Ed Harper


Just as an addition to this, if you are sending errors out to file, I found this https://www.simple-talk.com/sql/sql-tools/the-sqlcmd-workbench/

which I have used. If you omit setting the OUT, then you only get an error log created. So you have a command like this :

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

Then in MySQLBatchToRun.sql , something like this

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

In MySQLScript.sql you have the actual SQL to run. It's a bit convoluted, but works. The only issue I have is that it seems to create an empty error log file, even if there is not an error.

like image 27
PabloInNZ Avatar answered Sep 20 '22 15:09

PabloInNZ