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

asked Aug 19, 2021 at 1:10

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
answered Aug 19, 2021 at 1:38
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

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.