4

I have the following in a file called Test.sql:

SELECT '$(Val1)' AS [Value 1], '$(Val2)' AS [Value 2]

I am trying to run the following at the pwershell prompt:

Invoke-Sqlcmd -ServerInstance '.\SQLExpress' -Variable @("Val1='Test'","Val2='Test2'") -InputFile ".\SQL\Test.sql"

I get the error:

Invoke-Sqlcmd : Incorrect syntax near ''. At line:1 char:1 + Invoke-Sqlcmd -ServerInstance '.\SQLExpress' -Variable @("Val1='Test'","Val2='Te ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

If I swap out the SQL inside of Test.sql with:

SELECT @@VERSION

The powershell command line works and so I think the issue is with the sql file itself.

If I run sqlcmd.exe from a dos prompt providing the equivalent parameters my sql script works.

What do I need to do to get this to work?

3
  • Have you tries adding the DB name after .\SQLExpress? Commented Dec 1, 2014 at 17:39
  • Do you mean using the DATABASE switch? Commented Dec 1, 2014 at 17:39
  • I tried with the switch but makes no difference. Commented Dec 1, 2014 at 17:40

1 Answer 1

1

When I remove the single quotes from the SQL file it all works. Itt seems to work differently from the command prompt sqlcmd.exe.

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

2 Comments

Same here! Did you find any explanation about that?
Thank you thank you thank you! Was struggling to work out what I did wrong. Not sure why it works differently, the official documentation specifies single quotes for the value which doesn't work...

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.