Obtener la estructura de la tabla temporal (como generar script sql) y borrar la tabla temporal para la instancia actual


Cómo obtengo la estructura de la tabla temporal y luego elimino la tabla temporal. ¿Hay un sp_helptext para las tablas temporales? Finalmente, ¿es posible eliminar la tabla temporal en la misma sesión o ventana de consulta?

Ejemplo:

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

tempdb..sp_help #myTempTable

Referencia.

Author: Kiquenet, 2012-01-23

5 answers

Necesita usar comillas alrededor del nombre de la tabla temporal y puede eliminar la tabla temporal directamente después de usar 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
 93
Author: Mikael Eriksson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-24 06:50:16

Mientras sepa que no hay SP_HelpText para las tablas. Prueba esto:

Select * From tempdb.sys.columns Where object_id=OBJECT_ID('tempdb.dbo.#myTempTable');
 9
Author: Geri Reshef,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-24 08:48:24

Necesitaba poder recrear una tabla temporal en un script, así que utilicé este código para generar la parte columnas de la instrucción CREATE TABLE:

SELECT char(9) + '[' + c.column_name + '] ' + c.data_type 
   + CASE WHEN c.data_type IN ('decimal')
      THEN isnull('(' + convert(varchar, c.numeric_precision) + ', ' + convert(varchar, c.numeric_scale) + ')', '') 
      ELSE '' END
   + CASE WHEN c.IS_NULLABLE = 'YES' THEN ' NULL' ELSE '' END
   + ','
From tempdb.INFORMATION_SCHEMA.COLUMNS c 
WHERE TABLE_NAME LIKE '#myTempTable%' 

No probé todos los tipos de datos sql, pero esto funcionó para int, float, datetime, money y bit.

También - ApexSQL Complete (free) tiene una buena característica donde puede exportar los resultados de la cuadrícula a una instrucción Insert Into. Usé esto para cargar esta tabla temporal creada en mi script. ApexSQL Copie los resultados Como Insert into statement

 4
Author: dajo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-21 20:48:31

Exec sp_columns table_name;

Ejemplo

Exec sp_columns employees;

 3
Author: Ravi Kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-16 05:33:26
Select * From tempdb.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME LIKE '#yourtemp%'
 1
Author: flyreaver,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-30 06:00:47