sqlcmd options on print / display

helena Matos 61 Reputation points
2020-12-02T21:51:02.873+00:00

Hello

i need some help with sqlcmd utility at linux I have this select statement, but it display both at start with the -----

1> select 'grant select on ' + name + ' to READONLY' + CHAR(10) + 'go' from sysobjects where type = 'U' or type = 'V' or type = 'P'
2> go

------------------------------------------------------------------------------------------------------------------------------------

then a the end show the numbers

(34 rows affected)

how can the --- and the ( x rows affected) be ignored?
is there away in the sqlcmd to pass some parameter to ignore the lines?
thank you

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,852 questions
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 102.1K Reputation points MVP
    2020-12-02T22:46:25.547+00:00

    Use SET NOCOUNT ON in the SQL to suppress the rowcount.

    To suppress the header, use the option -h-1, for instance:

    sqlcmd -h-1 -Q"SET NOCOUNT ON SELECT @@version"
    
    2 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. AmeliaGu-MSFT 13,961 Reputation points Microsoft Vendor
    2020-12-03T02:41:20.403+00:00

    Hi @helena Matos ,
    As Erland mentioned, to remove dashed line header, you can use -h parameter with value -1, and to remove row count, you should use “SET NOCOUNT ON” in query.
    For example:

    sqlcmd -S wx-80490 -h -1  
    1> set nocount on; select 'grant select on ' + name + ' to READONLY' + CHAR(10) + 'go' from sysobjects where type = 'U' or type = 'V' or type = 'P'  
    2>go  
    

    And

    sqlcmd -S . -h -1 -Q "set nocount on; select 'grant select on ' + name + ' to READONLY' + CHAR(10) + 'go' from sysobjects where type = 'U' or type = 'V' or type = 'P'"  
    

    Please refer to the sqlcmd syntax for more details.

    Best Regards,
    Amelia


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments