Home Forums Programming Powershell How to find number of columns returned from query in powershell RE: How to find number of columns returned from query in powershell

  • Chand00 (7/25/2014)


    I want to find out number of columns returned from a query in a powershell script.

    As we will be passing a dynamic query which can retrieve only 2 columns or 5 columns or n columns based on user's choice.

    $SQLQuery = " select * from sysdatabases "

    $QueryResults = Invoke-Sqlcmd -Query $SQLQuery -ServerInstance Win7 -SuppressProviderContextWarning

    foreach ($row in $QueryResults)

    {

    $row.Item(0)

    }

    Can you please help on how to find number of columns returned from above query.

    My question would be... WHY is it necessary to do this in PowerShell instead of T-SQL?? :blink:

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)