Sqlcmd with jQuery in insert statements

I need to do a seeding of an MSSQL database and I have a lot of sql files to run ok.

Basically I have a start.sql file ...


PRINT 'First File'
:r "c:\sql\firstfile.sql"

PRINT 'Second File'
:r "c:\sql\secondfile.sql"


And I run this with sqlcmd -S LOCALHOST\SqlExpress -E -i "C:\sql\start.sql" -x

The problem is one of the files where I am inserting HTML data into the DB and it has script links that have jQuery in it $('#stuff')

. This is throwing syntax errors due to SQL variable substitution.

I was hoping the option -x

would stop the substitution, but I still have the error. Did I miss something?


source to share

1 answer

SQL command mode should be treated $(something)

as a variable.

In SQLCMD mode, the result PRINT '$test'

will be $test

, but execution PRINT '$(test)'

will return a message 'test' scripting variable not defined.

Using the -x ( lowercase x) option should NOT return this message though.

I verified this by running the following at the Windows command prompt:

sqlcmd -S localhost -E -i "c:\failsInSqlCmdMode.sql" -x


Where the .sql script contains the following SQL:

declare @test varchar(10) 
set @test = '$(#stuff)'
select @test




(1 rows affected)


It is only when I remove the -x from the end of the command that I get the error:

Sqlcmd: Error: Syntax error on line 2 next to command "#" in file "C: \ failInSqlCmdMode.sql".

I tested this on a Windows 7 x64 connection to SQL Server 2008 R2.



All Articles