Search code examples
sqlsql-serversqlcmd

Ignoring Scripting variables in sqlcmd (Importing database)


I am having a database import being done form a SQL file and i see my content have some data that is conflicting with sqlcmd import. The following queries are the 2 scenarios i am encountering in my import.

INSERT INTO [users] ([id],[email],[title],[desc]) VALUES (1096,'sda@asfsdf.com',N'Shami Doc', N'This is profile of Shami who sends $(mid) message');

In this query am getting the following error...

'mid' scripting variable not defined.

Now in the second case...

INSERT INTO [users] ([id],[email],[title],[desc]) VALUES (1091,'das@dsa.com',N'Raj Doc', N'This is profile of Raj who sends $(m message');

Am getting Syntax Error. In actual i just want both data to be imported to users table with the same data defined... i mean the $(mid) should be imported just as text into that column and also $(m should be imported as same.

I tried using escape character backslash and it did not help. Can someone help me in this ? How can i have both above queries run without loosing that data ?


Solution

  • Specify the -x SQLCMD command-line option to ignore scripting variables when executing scripts where scripting variables need to be retained as data. For example:

    SQLCMD -S YourServer -x -E -I -i YourScript.sql
    

    Note this example also includes the -I option to set QUOTED_IDENTIFIER ON, which is required for filtered indexes, indexed views and other features. SQLCMD initially sets QUOTED_IDENTIFIER OFF off by default for backwards compatibility.