How to get a list of columns from a temporary table?

I would like to get a list of columns in a temporary table, similar to the INFORMATION_SCHEMA.columns view. However, this code:

select * from tempdb.INFORMATION_SCHEMA.columns where TABLE_CATALOG = 'tempdb' and TABLE_NAME like '#myTemporaryTable%' 

returns one row per column and per session. Is it safe to do this:

 select distinct column_name,data_type from tempdb.INFORMATION_SCHEMA.columns where TABLE_CATALOG = 'tempdb' and TABLE_NAME like '#myTemporaryTable%' 

I have a feeling that this is not the case, even if you pull up a similar sentence so that it does not match myTemporaryTable and myTemporaryTable2.

+4
source share
1 answer

If you really need a tempdb request, I would use object_id

 SELECT * FROM tempdb.sys.columns WHERE object_id = OBJECT_ID('tempdb..#myTemporaryTable') 
+6
source

All Articles