How to retrieve field names from temporary table (SQL Server 2008)

Anthony picture Anthony · Apr 16, 2009 · Viewed 106.7k times · Source

I'm using SQL Server 2008. Say I create a temporary table like this one:

create table #MyTempTable (col1 int,col2 varchar(10))

How can I retrieve the list of fields dynamically? I would like to see something like this:

Fields:
col1
col2

I was thinking of querying sys.columns but it doesn't seem to store any info about temporary tables. Any ideas?

Answer

kristof picture kristof · Apr 16, 2009
select * from tempdb.sys.columns where object_id =
object_id('tempdb..#mytemptable');