Skip to content
Advertisement

How do I use a variable in an SQL script passed in from a powershell script

I’m new to Powershell and getting stuck with some of the syntax. I have a powershell script that calls a SQL script with a variable I want to access inside the script.

in my SQL query I want to be able to access the $powerShellVar variable like this

However, when I do that, I get this error

What am I doing wrong?

Advertisement

Answer

The parameter syntax is invalid. No wonder, as this is poorly documented. The -variable parameter assumes an array that contains name/value pairs like so,

For just a single parameter,

By this syntax, assuming printarg.sql contains select $(powerShellVar), this ought work:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement