SQL 如何将 UNION 查询结果移动到新表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6456388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 11:05:39  来源:igfitidea点击:

How to move UNION query results to a new table?

sqlsql-servertsqlsql-server-2008

提问by Ray

I have a sample query as shown below:

我有一个示例查询,如下所示:

SELECT     *
FROM         [#temp1]
UNION
SELECT     *
FROM         [#temp2]
UNION
SELECT     *
FROM         [#temp3]
UNION
SELECT     *
FROM         [#temp4]
UNION
SELECT     *
FROM         [#temp5]

How do I move this request of these queries into a new table? Note: My verison of SQL is:

如何将这些查询的这个请求移动到一个新表中?注意:我的 SQL 版本是:

Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (X64)   Mar 29 2009 10:11:52   Copyright (c) 1988-2008 Microsoft Corporation  Enterprise Edition (64-bit) on Windows NT 6.0 <X64> (Build 6002: Service Pack 2) 

I tried another Stackoverflow answer I found, i.e.

我尝试了另一个我找到的 Stackoverflow 答案,即

CREATE TABLE managers AS SELECT * FROM employees WHERE desg = 'MANAGER';

But I get an error of : Incorrect syntax near the keyword 'as'.

但我得到一个错误: Incorrect syntax near the keyword 'as'.

Here was my full query that failed with the above error:

这是我因上述错误而失败的完整查询:

CREATE TABLE #temp_UNION as


SELECT     *
FROM         [#temp1]
UNION
SELECT     *
FROM         [#temp2]
UNION
SELECT     *
FROM         [#temp3]
UNION
SELECT     *
FROM         [#temp4]
UNION
SELECT     *
FROM         [#temp5]

Any suggestions please on how I'm goofing up?

关于我是如何搞砸的,有什么建议吗?

Thank you, Ray

谢谢你,雷

回答by Chandu

In SQL Server you have to use

在 SQL Server 中,您必须使用

SELECT <COLUMNS_LIST>
  INTO <NEW_TABLE_NAME>
  FROM <TABLES, WHERE ETC>

More information @ http://msdn.microsoft.com/en-us/library/ms188029.aspx

更多信息@ http://msdn.microsoft.com/en-us/library/ms188029.aspx

Try this:

尝试这个:

SELECT *
  INTO  #temp_UNION 
FROM
(
        SELECT     *
    FROM         [#temp1]
    UNION
    SELECT     *
    FROM         [#temp2]
    UNION
    SELECT     *
    FROM         [#temp3]
    UNION
    SELECT     *
    FROM         [#temp4]
    UNION
    SELECT     *
    FROM         [#temp5]
) a

回答by Ovais Khatri

insert into temp_UNION
select * from (
SELECT     *
FROM         [#temp1]
UNION
SELECT     *
FROM         [#temp2]
UNION
SELECT     *
FROM         [#temp3]
UNION
SELECT     *
FROM         [#temp4]
UNION
SELECT     *
FROM         [#temp5]
)

回答by Madhivanan

or you dont need to use derived table. You can do this too

或者您不需要使用派生表。你也可以这样做

SELECT *   INTO  #temp_UNION  
FROM         [#temp1]     
UNION     
SELECT     *     FROM         [#temp2]     
UNION     SELECT     *     FROM         [#temp3]     
UNION     SELECT     *     FROM         [#temp4]     
UNION     SELECT     *     FROM         [#temp5]