Get list of columns in a temp table in SQL Server

John Joseph picture John Joseph · Dec 22, 2016 · Viewed 8.3k times · Source

I am creating a temporary table on-the-fly using Select * Into #temp from SomeChangingSource in a stored procedure. I need to then list the resulting columns.

Handling this for a regular (permanent) table is as simple as:

select COLUMN_NAME 
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='TableName'

How do I handle this for a temporary table?

Answer

John Cappelletti picture John Cappelletti · Dec 22, 2016

Your were close. Just needed to point it to Tempdb.Sys.Columns

 Select * From  Tempdb.Sys.Columns Where Object_ID = Object_ID('tempdb..#TempTable')