Search code examples
sqlkshsqlcmd

SQLCMD is appending newline and "1" in the query results


I am calling sqlcmd utility from ksh script to retrieve dummy record and store it in a variable

MY_VAR=`sqlcmd -S server,port -U username -P password -d dbname -h -1 -W -q "SET NOCOUNT ON; select 'dummy';"`

I'm expecting MY_VAR to contain "dummy" as it is the row returned from the query, also -h -1 is used to remove the header and -W to remove the trailing spaces. But instead, the output is

dummy
1

I don't know where 1 is coming. I'm firing the query itself and its correctly giving me "dummy" as result.


Solution

  • You need to use the uppercase -Q. And you can skip both ; I think.

    > SQLCMD.EXE -E -S .\SQLEXPRESS -W -h -1 -Q "SET NOCOUNT ON select 'dummy'"
    dummy