Search code examples
sql-serversqlcmd

T-SQL error if use dollar and bracket symbols together


I'm getting the following error message with execution T-SQL script with this construction "$(", in sqlcmd utility:

C:\Users\Admin>sqlcmd -S DESKTOP-5AA9JIA\SQLEXPRESS -q "select '$(' " -R
Sqlcmd: Error: Syntax error at line 1 near command '''.
1>

if run this script via SSMS then everything works smoothly.

Getting the same error, when using INSERT INTO statement or any other statement.

Any suggestion on how to resolve it?


Solution

  • You should use -x when launch sqlcmd to ignore scripting variables.

    -x

    Causes sqlcmd to ignore scripting variables. This is useful when a script contains many INSERT statements that may contain strings that have the same format as regular variables, such as $(variable_name).

    sqlcmd Utility

    When sqlcmd parser sees $(it expects scripting variable that is not provided so it throws the error.

    Here is my test:

    enter image description here