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

RetroCoder picture RetroCoder · Jan 23, 2012 · Viewed 81.5k times · Source

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 window?

Example:

select *
into #myTempTable  -- creates a new temp table
from tMyTable  -- some table in your database

tempdb..sp_help #myTempTable

Reference.

Answer

Mikael Eriksson picture Mikael Eriksson · Jan 24, 2012

You need to use quotes around the temp table name and you can delete the temp table directly after using drop table ....

select *
into #myTempTable  -- creates a new temp table
from tMyTable  -- some table in your database

exec tempdb..sp_help '#myTempTable'

drop table #myTempTable