Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when inserting strings with $( with Invoke-SqlCmd in Powershell

Setup:

CREATE TABLE MyTest (TestCol1 nchar(5))

Test:

Following work:

Invoke-Sqlcmd -Database "databasename" -ServerInstance "hostname" -OutputSqlErrors $True -Query "INSERT INTO MyTest VALUES ('`$5')"
Invoke-Sqlcmd -Database "databasename" -ServerInstance "hostname" -OutputSqlErrors $True -Query "INSERT INTO MyTest VALUES ('(5')"

Following fails with the error below:

Invoke-Sqlcmd -Database "databasename" -ServerInstance "hostname" -OutputSqlErrors $True -Query "INSERT INTO MyTest VALUES ('`$(5')"
Invoke-Sqlcmd -Database "databasename" -ServerInstance "hostname" -OutputSqlErrors $True -Query "INSERT INTO MyTest VALUES ('`$`(5')"

Error: Invoke-Sqlcmd : At line:1 char:1 + Invoke-Sqlcmd -Database "databasename" -ServerInstance "hostname" -Ou ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ParserError: (:) [Invoke-Sqlcmd], ParserException + FullyQualifiedErrorId : ExecutionFailureException,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

Aftersome research I found that $( provides functionality in powershell thus is reserved. However I tried escaping the parenthesis but with no success. I've tried finding alternatative. Any ideas on how I can do this? If I use the CHAR function in SQL Server that works but it would be a pain to deal with in my code. Thank you.

like image 391
Russ960 Avatar asked Nov 04 '22 01:11

Russ960


1 Answers

All you need to do is disable the variables lookup in the invoke-Sqlcmd by adding -DisableVariables. $() is use to pass in variable into the sql statements.

Invoke-Sqlcmd -InputFile $fileName -ServerInstance $serverInstance -DisableVariables

Source http://msdn.microsoft.com/en-us/library/cc281720.aspx

like image 94
grasshope Avatar answered Nov 15 '22 06:11

grasshope