4

I'd like to get the 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 it isn't, even if you tighten up the like clause so it won't match myTemporaryTable and myTemporaryTable2.

4

1 回答 1

6

如果你真的需要查询 tempdb,我会使用 object_id

SELECT  *
FROM    tempdb.sys.columns 
WHERE object_id = OBJECT_ID('tempdb..#myTemporaryTable')
于 2013-05-01T17:16:07.940 回答