Saving value into a variable in PS RRS feed

  • Question

  • Hi All.

    I am tryin to save a value from the database into a Ps variable, however its not working.

    $result = invoke-Sqlcmd2 -ServerInstance "xxxx" -Database database_name -Query "select request_active from requests where active_request = 1 " 

    My column request_active is a bit column and I have tried different methods to get it to work to no avail. Lastly, I am only using invoke-sqlcmd2 because the server is SQL 2008 R2 on windows 2008 R2, and it does not have invoke-sqlcmd installed by default.

    Thanks in advance.

    Friday, March 2, 2018 11:37 AM

All replies

  • Your problem is not saving into a variable; your problem is, that your Invoke-Sqlcmd2 is returning nothing. Saving into a variable is quite simple:
    $a = "b"
    Friday, March 2, 2018 1:13 PM
  • Invoke-SqlCmd returns a table object and not a single value.

    $sql = 'select request_active from requests where active_request = 1'
    if($result = Invoke-SqlCmd -ServerInstance "xxxx" -Database database_name -Query $sql){
        Write-Host 'no data returned'

    We don't know what "Invoke-SqlCmd2" is Use the PowerShell SQLServer module instead:

    Install-Module SQLServer


    Friday, March 2, 2018 3:40 PM