0

I am trying to pass a variable into a SQL update statement to update a table column. I run the following commands and do not receive any errors. However, the table column was not updated.

$SQLserver = 'servername'
$Database = 'databasename'

$variable = (Get-Content -path \\share\test\testfile.json | ConvertFrom-Json)

$vm = Invoke-SQLCMD -ServerInstance $SQLServer -Database $Database -Query "Update (tablename) set (columnname) = 1 where vmid IN ('$variable.attribute')"

I see that all the data is being passed into the variable. But, the update statement is not updating my column. Also, I do not receive any error messages when running my code.

1
  • 1
    Simple variables expand in double quotes. To access properties of a variable, enclose in a subexpression $(). Check the duplicate. Commented Dec 11, 2022 at 0:48

1 Answer 1

-1

you were too close, only "$" missing, here's the correct code

$SQLserver = 'servername'
$Database = 'databasename'

$variable = (Get-Content -path \\share\test\testfile.json | ConvertFrom-Json)

$vm = Invoke-SQLCMD -ServerInstance $SQLServer -Database $Database -   Query "Update $tablename set $columnname = 1 where vmid IN ('$variable.vmid')"
Sign up to request clarification or add additional context in comments.

1 Comment

You should test before you post an answer. This is not a valid answer currently.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.