0

How do I update a PowerShell variable using an Invoke-Sqlcmd statement?

Eg.

$PSVariable

Import-Module sqlserver

$QueryStatement = "SELECT $PSVariable = Max(Data) FROM MyDatabase"
    
Invoke-Sqlcmd -ServerInstance "MyServer\Instance" -Query $QueryStatement

Not sure what is off with this code. I am new to the PowerShell sqlserver module

1 Answer 1

4

give this a go...

$Query = "SELECT Max(Data) as MaxData FROM MyDatabase"
$Result = Invoke-Sqlcmd -ServerInstance "MyServer\Instance" -Query $Query | select -expand MaxData 

write-host $Result
1
  • 1
    This did answer my question. Is there a way I can store my result to a SQL variable like Declare @MyVariable int; then update that and return the result? Commented Aug 19, 2021 at 3:11

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.