Get structure of temp table (like generate sql script) and clear temp table for current instance

前端 未结 7 1990
粉色の甜心
粉色の甜心 2021-01-30 19:56

How do I get structure of temp table then delete temp table. Is there a sp_helptext for temp tables? Finally is it possible to then delete temp table in same session or query

7条回答
  •  孤独总比滥情好
    2021-01-30 20:26

    So, this helped me. It created the Table columns.

    Select Column_Name + ' [' + DATA_TYPE + ']' + 
    case when Data_Type in ('numeric', 'varchar', 'char')
        then '(' +
            case
                when DATA_TYPE = 'numeric' then CAST(numeric_precision as varchar(3)) + ',' + CAST(numeric_scale as varchar(3))
                when DATA_TYPE = 'varchar' then CAST(CHARACTER_MAXIMUM_LENGTH as varchar(3))
                when DATA_TYPE = 'char' then CAST(CHARACTER_MAXIMUM_LENGTH as varchar(3))
            end
             + ')'
        else ''
    end
    + ','
    , * 
    From tempdb.INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_NAME LIKE '#MEHTEMPTABLE%'
    

    All I then needed to do was copy these items into a Table Declaration

    Declare @MyTable Table
    (
    --All columns here
    )
    

    That would've resolved my issue, but I was pressed for time

提交回复
热议问题