SQLCMD syntax error when running script that works fine in management studio

When I run this select '$(''test'')'in SQL Management Studio 2008, it returns$('test')

When I run sqlcmd -S SERVER -E -d DATABASE -q "select $(''test'')"on the command line, it returnsSqlcmd: Error: Syntax error at line 1 near command '''.

If I remove the dollar sign, it will work. Is "$" a special character?

Is this sqlcmd error? How can I modify the script to get the desired result.

+3
source share
2 answers

Yes, it $(id)has special semantics in SQLCMD: it is a variable substitution. You can run commands, for example:

sqlcmd /E /S . /v variable=MyTable /Q "select * from $(variable)"

MyTable. , /v - . SSMS, , SQL . SSMS , " SQLCMD".

. sqlcmd .

+14

, :

select $('test')

. , "$", :

select ('test')

, , ", :

select '$(test)'

:

sqlcmd -S SERVER -E -d DATABASE -q "select ''$(test)''"
0

Source: https://habr.com/ru/post/1737401/


All Articles