5

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.

0

5 Answers 5

6

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

Sign up to request clarification or add additional context in comments.

Comments

1

try this:

'INSERT INTO MyTest VALUES ("$(5")'

or this:

'INSERT INTO MyTest VALUES (''$(5'')'

9 Comments

To provide a reason why this works: In PowerShell, double-quoted strings ("hello") are parsed for variables. Single-quoted strings ('hello') are treated as literal strings and are never parsed. Try the following: $x = 0; "$x"; '$x'. That said, your first example "INSERT INTO MyTest VALUES ('`$(5')" should be working. That is a correct string.
@BaconBits I think this comment was better in the OP, not in my answer. Or is for me this comment? :)
If I run it this way it is expecting more characters with the >> prompt.
@C.B. No, not really for you. It just isn't obvious why a single quote would work but a double wouldn't. I was expanding on your answer. The verbiage is a bit weird though. It should say "That said, the first example... ". :)
@Russ960 What are your database and server names? Do they have any reserved Powershell characters in them?
|
0

The $(anyvalue) are reserved variables to be used with SQLCMD.EXE Change the symbol $ by the sql server CHAR(36):

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

1 Comment

That is what I determined and posted here previously.
-1

The issue seems that the $( is a reserved word in Powershell thus to make this work I had use the CHAR function to store the $ (CHAR(36)). Once I did this it worked but for this project I abandoned using the Invoke-SqlCmd command and used a standard ADO.NET method. It seems that the Invoke-SqlCmd wants to parse the command and for the reserved combination continues to see the reserved word even if you escape the characters.

Comments

-2
"INSERT INTO MyTest VALUES ('`$5')"

Outputs a string

INSERT INTO MyTest VALUES ('$5')

"INSERT INTO MyTest VALUES ('(5')"

Outputs a string

INSERT INTO MyTest VALUES ('(5')

"INSERT INTO MyTest VALUES ('`$(5')"

Outputs a string

INSERT INTO MyTest VALUES ('$(5')

"INSERT INTO MyTest VALUES ('`$`(5')"

Outputs a string

INSERT INTO MyTest VALUES ('$(5')

What do you want to insert? Actually '$5' or the value of the variable $5? Also, you have strange parentheses around your variable $5. Not sure if is suppose to be like that or your don't understand another important part of powershell variables? Because this a variable in a string "There are $((Get-Process).Count) processes running" too.

2 Comments

I am trying to insert $(5. Don't ask me why I'm moving data from one system to another.
'INSERT INTO MyTest VALUES ("$(5")' or "INSERT INTO MyTest VALUES (<backtick>"<backtick>$(5)<backtick>")". Hope I helped and not confused.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.