74

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?

7 Answers 7

138
select * from tempdb.sys.columns where object_id =
object_id('tempdb..#mytemptable');
2
  • Good ans. Use select name from tempdb.sys.tables get all the temporary table names
    – Raaghav
    Jul 26, 2016 at 7:35
  • 3
    Verified that this method doesn't return data from #temp tables created via a different connection. The alternatives using LIKE #temp% do.
    – Morvael
    Dec 20, 2017 at 8:09
31
SELECT * 
FROM tempdb.INFORMATION_SCHEMA.COLUMNS
WHERE table_name LIKE '#MyTempTable%'
5
  • 4
    This answer is better. The table name is included as well as the datatype for the column. If your where clause contains a list of temp tables, then this is even more superior because you can differentiate which columns come from what temp table
    – VISQL
    Oct 31, 2012 at 18:08
  • 1
    @VISQL Actually, the accepted answer is better because it does not use INFORMATION_SCHEMA. You can easily get the datatypes by JOINing to sys.types and you can easily get the table names via OBJECT_NAME( object_id , database_id ) or JOINing to tempdb.sys.tables. This is all that the INFORMATION_SCHEMA Views do. Apr 2, 2016 at 5:54
  • 1
    The problem with this is that if you are using 2 temp tables with similar names (e.g #TEMP1 and #TEMP12) then when using #TEMP1 as the table name you also get the columns for #TEMP12
    – apc
    Mar 22, 2017 at 15:26
  • Using '#MyTempTable[][][_]%' works better to ensure you don't get other tables whcih start with name of the table you are looking for
    – apc
    Mar 22, 2017 at 15:30
  • 2
    Downvoted as: Verified that the accepted answer doesn't return data from #temp tables created via a different connection. This method does. The accepted answer is much safer.
    – Morvael
    Dec 20, 2017 at 8:07
8

To use information_schema and not collide with other sessions:

select * 
from tempdb.INFORMATION_SCHEMA.COLUMNS
where table_name =
    object_name(
        object_id('tempdb..#test'),
        (select database_id from sys.databases where name = 'tempdb'))
7

The temporary tables are defined in "tempdb", and the table names are "mangled".

This query should do the trick:

select c.*
from tempdb.sys.columns c
inner join tempdb.sys.tables t ON c.object_id = t.object_id
where t.name like '#MyTempTable%'

Marc

3
  • 2
    I think this could cross scopes. If it's one time code, fine. If it's code that will have a real lifespan, problem.
    – jcollum
    Jul 28, 2009 at 19:32
  • 1
    Yep, bad solution - if multiple connections created temp tables with the same name (eg in a proc that gets called from an application), this will be wrong.
    – Tao
    Apr 13, 2011 at 12:37
  • Downvoted as: Verified that the accepted answer doesn't return data from #temp tables created via a different connection. This method does. The accepted answer is much safer.
    – Morvael
    Dec 20, 2017 at 8:08
3

you can do it by following way too ..

create table #test (a int, b char(1))

select * From #test

exec tempdb..sp_columns '#test'
1
  • provides everything needed to write the explicit CREATE TABLE: exec tempdb..sp_columns '#test'
    – yzorg
    Apr 6, 2015 at 14:15
1

Anthony

try the below one. it will give ur expected output

select c.name as Fields from 
tempdb.sys.columns c
    inner join tempdb.sys.tables t
 ON c.object_id = t.object_id
where t.name like '#MyTempTable%'
0
select * 
from tempdb.INFORMATION_SCHEMA.COLUMNS 
where TABLE_NAME=OBJECT_NAME(OBJECT_ID('#table'))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.