Insertar Datos En La Tabla Temporal con Consulta


Tengo una consulta existente que genera datos actuales, y me gustaría insertarlos en una tabla Temporal, pero tengo algunos problemas al hacerlo. ¿Alguien tendría alguna idea de cómo hacer esto?

Aquí hay un ejemplo

SELECT *
FROM  (SELECT Received,
              Total,
              Answer,
              ( CASE
                  WHEN application LIKE '%STUFF%' THEN 'MORESTUFF'
                END ) AS application
       FROM   FirstTable
       WHERE  Recieved = 1
              AND application = 'MORESTUFF'
       GROUP  BY CASE
                   WHEN application LIKE '%STUFF%' THEN 'MORESTUFF'
                 END) data
WHERE  application LIKE isNull('%MORESTUFF%', '%') 

Esto parece generar mis datos actualmente de la manera que los necesito, pero me gustaría pasarlos a una tabla Temporal. Mi problema es que soy bastante nuevo en las consultas SQL y no he sido capaz de encontrar una manera de hacerlo. O si es posible. Si no lo es posible, ¿hay una mejor manera de obtener los datos que estoy buscando WHERE application LIKE isNull('%MORESTUFF%','%') en una tabla temporal?

Cualquier ayuda sería muy apreciada! ¡Gracias!

Author: marc_s, 2013-11-21

8 answers

SELECT *
INTO #Temp
FROM

  (SELECT
     Received,
     Total,
     Answer,
     (CASE WHEN application LIKE '%STUFF%' THEN 'MORESTUFF' END) AS application
   FROM
     FirstTable
   WHERE
     Recieved = 1 AND
     application = 'MORESTUFF'
   GROUP BY
     CASE WHEN application LIKE '%STUFF%' THEN 'MORESTUFF' END) data
WHERE
  application LIKE
    isNull(
      '%MORESTUFF%',
      '%')
 122
Author: Yosi Dahari,
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-11-20 22:03:37

SQL Server R2 2008 necesita la cláusula AS de la siguiente manera:

SELECT * 
INTO #temp
FROM (
    SELECT col1, col2
    FROM table1
) AS x

La consulta falló sin el AS x al final.


EDITAR

También es necesario cuando se utiliza SS2016, tuvo que añadir as t al final.

 Select * into #result from (SELECT * FROM  #temp where [id] = @id) as t //<-- as t
 96
Author: Shaun Luttin,
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-05-28 11:44:59

La forma más rápida de hacer esto es usando el comando "SELECCIONAR EN", por ejemplo,

SELECT * INTO #TempTableName
FROM....

Esto creará una nueva tabla, no tiene que crearla por adelantado.

 22
Author: Yuriy Galanter,
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-11-20 21:57:55

Puedes hacer eso así:

INSERT INTO myTable (colum1, column2)
SELECT column1, column2 FROM OtherTable;

Solo asegúrese de que las columnas coincidan, tanto en número como en tipo de datos.

 8
Author: wvdz,
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-11-20 21:57:54

Prueba esto:

SELECT *
INTO #Temp
FROM 
(select * from tblorders where busidate ='2016-11-24' and locationID=12
) as X

Utilice alias con x para que no falle el script y el resultado.

 3
Author: Alok Sharma,
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-11-30 19:10:17

Personalmente, necesitaba un poco de mano para averiguar cómo usar esto y es realmente increíble.

SELECT *
    INTO #TEMP
    FROM (
    The query you want to use many times
    ) AS X

SELECT * FROM #TEMP WHERE THIS = THAT
SELECT * FROM #TEMP WHERE THIS <> THAT
SELECT COL1,COL3 FROM #TEMP WHERE THIS > THAT

DROP TABLE #TEMP
 2
Author: bteague,
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
2017-07-20 17:59:45
SELECT * INTO #TempTable 
FROM SampleTable
WHERE...

SELECT * FROM #TempTable
DROP TABLE #TempTable
 1
Author: Saqib A. Azhar,
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
2017-10-06 12:37:46

Esto es posible. Prueba de esta manera:

Create Global Temporary Table 
BossaDoSamba 
On Commit Preserve Rows 
As 
select ArtistName, sum(Songs) As NumberOfSongs 
 from Spotfy 
    where ArtistName = 'BossaDoSamba'
 group by ArtistName;
 0
Author: Luiz Henrique Lima,
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-08 11:15:00