Is there a way to run sqlcmd to get query output without showing the number of rows affected?
I think you might want the "SET NOCOUNT ON" option. Your SQL script will look like:
set nocount on
select x, y, z from agent
set nocount off
And the results set will be:
24 aingles1 creablegs
25 tbails12 bull2dog12
26 jtaylor3 Leandon62606
27 forrestw1 nuke19211
Minus the count of rows line at the end.
sqlcmd -S svr -d db ... | findstr /v "rows affected"
findstr
is built into the OS and is simliar to grep. See findstr /? for more options.
To remove a column, use cut.exe
tool. So you could run:
sqlcmd -S svr -d db ... | cut -c10-
This would only output from character 10 onwards, thus removing the line numbers. Adjust the number 10 accordingly. You could also try fields:
sqlcmd -S svr -d db ... | cut -f2-
This would only output fields 2 and onwards (to the right).
I add the following code block to the beginning of the query itself to remove all messages when using sqlcmd.exe to export results to CSV.
-- Suppress warnings and messages like (2 rows effected)
SET ANSI_WARNINGS OFF;
SET NOCOUNT ON;
-- SQLCMD.exe batch command
:setvar SQLCMDERRORLEVEL 1
-- To Reenable
--:setvar SQLCMDERRORLEVEL 0
/**********************************************************************************
** DISABLING WARNINGS AND MESSAGES
** Disable "Changed database context to 'DatabaseName'." message in the CSV file when using
** SQLCMD.exe. You'll get a syntax error when executing this query in Management
** Studio if SQLCMD Mode is not enabled [Query] --> [SQLCMD Mode].
** :setvar SQLCMDERRORLEVEL 1 is used to disable messages like (152 Rows affected).
**********************************************************************************/