How do I return column headers when there are no rows returned with Invoke-Sqlcmd?

StackOverflow https://stackoverflow.com/questions/19984799

  •  30-07-2022
  •  | 
  •  

문제

When executing a query using Invoke-Sqlcmd, if no rows are returned, the result is just $null.

I wondering if it's possible to still return the column headers, but with no data. This would make it consistent with a query which did return rows.

Is there an easy way to do this?

도움이 되었습니까?

해결책

If you get a null back try this query:

select [COLUMN_NAME] from information_schema.columns where TABLE_NAME='TABLENAME' 

다른 팁

Check for $null - if $null then call this query:

   set fmtonly on
   select * from yourTable where 1=0
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top