February 21, 2008 at 10:37 am
How to use double quotes to identify column name within a sqlcmd query using -q
Ex:
sqlcmd -S srvr -q "select "first name" from pubs..Names"
This gives an error
'Sqlcmd: 'first name" from pubs..Names"': Unexpected argument.
Is there an escape sequence for the ' " ' character. Using \"first name\" does not work.
Thanks
February 21, 2008 at 10:53 am
Not real sure with sqlcmd but if first name is a variable in vb you would enter
sqlcmd -S srvr -q "select '" & first_name & "' from pubs..Names"
February 21, 2008 at 11:06 am
Not using VB..
am running the command directly from windows(cmd)
C:\> sqlcmd -S srvr -q "select "first name" from pubs..Names"
February 21, 2008 at 11:08 am
February 26, 2008 at 4:04 pm
Please note.. the column is "first name", not first_name (which does not need double quotes)
Besides.. I'm running this from local cmd prompt.. not on the 'server' (where xp_cmdshell doesn't work)
Is there an escape sequence for the " character? ( '\' doesn't work)
C:\> sqlcmd -S srvr -q "select \"first name\" from pubs..Names" -- ERROR
Thanks
February 27, 2008 at 7:19 am
use square brackets instead of "
hope this helps
February 27, 2008 at 11:20 am
Thanks.. But
'[]' works for column names, but another statement i run from cmd is :
sqlcmd -S srvr -Q "exec sp_oamethod @oSrv, 'Databases("pubs").Tables("T_Names").Script(2,"c:ames.sql")"
for which double quotes are a must in the syntax!
There is a workaround for sqlcmd: Instead of -Q "t-sql", manually put the t-sql query in a file (input.sql) and then use -i "input.sql"
However, it kind of defeats the goal of total automation..
February 27, 2008 at 12:19 pm
Use it as [first name] instead of using as "first name"
Cheers,
Sugeshkumar Rajendran
SQL Server MVP
http://sugeshkr.blogspot.com
February 27, 2008 at 1:05 pm
Thanks.. Sugesh. I tried that and it works.. but please see the comment:
Thanks.. But
'[]' works for column names, but another statement i run from cmd is :
sqlcmd -S srvr -Q "exec sp_oamethod @oSrv, 'Databases("pubs").Tables("T_Names").Script(2,"c:ames.sql")"
for which double quotes are a must in the syntax!
There is a workaround for sqlcmd: Instead of -Q "t-sql", manually put the t-sql query in a file (input.sql) and then use -i "input.sql"
However, it kind of defeats the goal of total automation..
June 21, 2012 at 7:02 am
Hi,
I just encountered the same issue and I believe the way to escape the quote within the -q or -Q is to double up the quotes.
So you would want to REPLACE(<yourstring>, '"', '""')
For example
C:\Documents and Settings\ay9805>sqlcmd -S .\abair -Q"SELECT '"error"' as text"
Sqlcmd: 'error"' as text"': Unexpected argument. Enter '-?' for help.
C:\Documents and Settings\ay9805>sqlcmd -S .\abair -Q"SELECT '""error""' as text
"
text
-------
"error"
(1 rows affected)
Hope this helps,
-Michael Abair
November 24, 2014 at 11:55 am
Thanks for that, the double double quotes worked for me!
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply