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

前端 未结 7 1775
借酒劲吻你
借酒劲吻你 2020-12-01 01:01

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 r

相关标签:
7条回答
  • 2020-12-01 01:33

    you can do it by following way too ..

    create table #test (a int, b char(1))
    
    select * From #test
    
    exec tempdb..sp_columns '#test'
    
    0 讨论(0)
  • 2020-12-01 01:36

    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 讨论(0)
  • 2020-12-01 01:47
    select * from tempdb.sys.columns where object_id =
    object_id('tempdb..#mytemptable');
    
    0 讨论(0)
  • 2020-12-01 01:47

    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'))
    
    0 讨论(0)
  • 2020-12-01 01:48

    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

    0 讨论(0)
  • 2020-12-01 01:50
    select * 
    from tempdb.INFORMATION_SCHEMA.COLUMNS 
    where TABLE_NAME=OBJECT_NAME(OBJECT_ID('#table'))
    
    0 讨论(0)
提交回复
热议问题