SELECCIONAR EN USANDO LA CONSULTA DE UNIÓN


Quiero crear una nueva tabla en SQL Server con la siguiente consulta. No puedo entender por qué esta consulta no funciona.

Query1: Works

SELECT * FROM TABLE1
UNION
SELECT * FROM TABLE2 

Query2: No Funciona. Error: Msg 170, Level 15, State 1, Line 7 Line 7: Incorrect syntax near ')'.

SELECT * INTO [NEW_TABLE]
FROM
(
SELECT * FROM TABLE1
UNION
SELECT * FROM TABLE2
)

Gracias!

Author: OMG Ponies, 2010-10-26

4 answers

Debe definir un alias de tabla para una tabla derivada en SQL Server:

SELECT x.* 
  INTO [NEW_TABLE]
  FROM (SELECT * FROM TABLE1
        UNION
        SELECT * FROM TABLE2) x

"x" es el alias de la tabla en este ejemplo.

 59
Author: OMG Ponies,
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
2010-10-25 20:49:28

También puedes probar:

create table new_table as
select * from table1
union
select * from table2
 1
Author: Pablo Santa Cruz,
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
2010-10-25 20:51:03
select *
into new_table
from table_A
UNION
Select * 
From table_B

Esto solo funciona si Table_A y Table_B tienen los mismos esquemas

 1
Author: Jim,
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
2010-10-25 20:56:29
INSERT INTO #Temp1
SELECT val1, val2 
FROM TABLE1
 UNION
SELECT val1, val2
FROM TABLE2
 0
Author: Lorena Pita,
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-07-31 20:58:34