11

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?

John Cappelletti
  • 79,615
  • 7
  • 44
  • 66
John Joseph
  • 1,003
  • 1
  • 10
  • 20
  • Does this answer your question? [How to retrieve field names from temporary table (SQL Server 2008)](https://stackoverflow.com/questions/756080/how-to-retrieve-field-names-from-temporary-table-sql-server-2008) – casperOne May 12 '23 at 18:48
  • The question was perfectly answered 7 years ago. – John Joseph May 12 '23 at 22:25
  • The comment is automatically generated when I voted to close as a dupe. – casperOne May 14 '23 at 00:26

1 Answers1

33

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

 Select * From  Tempdb.Sys.Columns Where Object_ID = Object_ID('tempdb..#TempTable')
John Cappelletti
  • 79,615
  • 7
  • 44
  • 66