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

  • Jeff Moden (7/25/2014)


    Eirikur Eiriksson (7/25/2014)


    Jeff Moden (7/25/2014)


    Chand00 (7/25/2014)


    Hi,

    Thank you. Below one worked for me.

    $QueryResults[0].Table.Columns.Count

    Apologies. I tried to post but I was unable to post somehow. Once again Thankyou

    Still, why does this need to be done from PowerShell? What is the overall task that you're trying to accomplish?

    Just to supply the T-SQL answer to the thread, the stored procedure sp_describe_first_result_set does the job.

    😎

    So does a trip to sys.all_columns or sys.columns or INFORMATION_SCHEMA.columns (to name a couple of methods).

    I still want to know why someone needs to do this from PowerShell. 😉

    I think this could possibly be a case of choosing the tool before the task but then again how could I know:-)

    The procedure sp_describe_first_result_set does more than the meta data queries, if I remember correctly, it uses openrowset with the system only TABLE directive to retrieve the structure of the query results. It was introduced in SQL Server 2012 where sp_describe_first_result_set, sp_describe_undeclared_parameters, sys.dm_exec_describe_first_result_set, and sys.dm_exec_describe_first_result_set_for_object replace the SET FMTONLY directive.

    😎