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.

whitemagic
  • 45
  • 3
  • 1
    Simple variables expand in double quotes. To access properties of a variable, enclose in a subexpression `$()`. Check the duplicate. – Doug Maurer Dec 11 '22 at 00:48

1 Answers1

-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')"
Andres Ospina
  • 108
  • 1
  • 2
  • 8