Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress hyphens in SQLCMD

How can I suppress hyphens (------------) from the results set of this sqlcmd command:

C:\temp>sqlcmd -d AdventureWorks -s ";"   -Q "SET NOCOUNT ON SELECT top 5 FirstName, LastName FROM Person.Contact;" FirstName                                         ;LastName --------------------------------------------------;---------------------------- Gustavo                                           ;Achong Catherine                                         ;Abel Kim                                               ;Abercrombie Humberto                                          ;Acevedo Pilar                                             ;Ackerman  C:\temp> 
like image 476
atricapilla Avatar asked Mar 02 '10 09:03

atricapilla


People also ask

Can we use hyphen in SQL query?

No, the hyphen is an operator, and you can't use that in the middle of an identifier.

What is hyphen in SQL?

The double hyphen places a single-line comment in a SQL*Plus script. The double hyphen works the same way as REMARK, except that it may be used in SQL statements and PL/SQL blocks. When used in a SQL statement or PL/SQL block, the double hyphen may be used to add trailing comments to a line.

How do I use SQLCMD mode?

To switch a Database Engine Query Editor window to SQLCMD mode. In Object Explorer, right-click the server, and then click New Query, to open a new Database Engine Query Editor window. On the Query menu, click SQLCMD Mode. The Query Editor executes sqlcmd statements in the context of the Query Editor.

What is the purpose of using SQLCMD Q command?

The sqlcmd utility lets you enter Transact-SQL statements, system procedures, and script files through a variety of available modes: At the command prompt. In Query Editor in SQLCMD mode.


1 Answers

I didn't see all this info in once place and thought the next person looking for it might appreciate it...

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.

Example:

SQLCMD -S 127.0.0.1\SQL_SERVER_Instance -d db_name -U db_login -P password -i your_script.sql -o your_output.csv -h -1 

In your SQL script:

SET NOCOUNT ON -- removes (rows affected from the output) select 'your_column_1, your_column_2' select * from your_table 

You don't need to use a union between your select statements for this.

like image 171
Justin Hanley Avatar answered Sep 23 '22 19:09

Justin Hanley