可以将SELECT INTO子句与UNION [ALL]一起使用吗?


154

在SQL Server中,这会将来自Customers表的100条记录插入到tmpFerdeen中:

SELECT top(100)*
INTO tmpFerdeen
FROM Customers

是否可以跨UNION ALL SELECT进行SELECT INTO:-

SELECT top(100)* 
FROM Customers
UNION All
SELECT top(100)* 
FROM CustomerEurope
UNION All
SELECT top(100)* 
FROM CustomerAsia
UNION All
SELECT top(100)* 
FROM CustomerAmericas

不太确定在哪里添加INTO子句。


您确定需要全部工会吗?
sfossen

是。由于记录在所有表中都是唯一的。
Ferdeen

Answers:


214

这在SQL Server中有效:

SELECT * INTO tmpFerdeen FROM (
  SELECT top 100 * 
  FROM Customers
  UNION All
  SELECT top 100 * 
  FROM CustomerEurope
  UNION All
  SELECT top 100 * 
  FROM CustomerAsia
  UNION All
  SELECT top 100 * 
  FROM CustomerAmericas
) as tmp

1
这也适用于SELECT前100名*从客户UNION全部进入tmpFerdeen所有SELECT前100名*从CustomerEurope UNION全部选择SELECT前100 * FROM CustomerAsia UNION所有SELECT前100名* FROM CustomerAmericas(抱歉,无法在此处格式化sql)。谢谢!
Ferdeen

7
“ as tmp”的含义是什么?
戴夫

@chrisVanOpstal,您为什么选择前100名?当我们选择所有记录时,它会给出以下错误:“除非在视图,内联函数,派生表,子查询和公共表表达式中使用ORDER BY子句,否则也必须指定TOP或FOR XML。” 请给出一些解决方案。
ShaileshDev

1
嗨,@ Dave询问了什么,我看到删除“ as tmp”会导致错误“语法错误,期望as,id或quoted_id”。那么它是用来干什么的呢?
Ravid Goldenberg

4
@ Dave,petric:在SQL Server中,需要给临时表起一个名字。就这样。tmp除了使SQL语句有效之外,不提供任何其他功能。例如,在Oracle SQL上,这是可选的。
Wouter

130

为此,您根本不需要派生表。

放在第一个INTO之后SELECT

SELECT top(100)* 
INTO tmpFerdeen
FROM Customers
UNION All
SELECT top(100)* 
FROM CustomerEurope
UNION All
SELECT top(100)* 
FROM CustomerAsia
UNION All
SELECT top(100)* 
FROM CustomerAmericas

2
我不同意-尽管以上答案也是正确的,但可接受的答案意图更明确
耐力赛

5
SELECT * INTO tmpFerdeen FROM 
(SELECT top(100)*  
FROM Customers 
UNION All 
SELECT top(100)*  
FROM CustomerEurope 
UNION All 
SELECT top(100)*  
FROM CustomerAsia 
UNION All 
SELECT top(100)*  
FROM CustomerAmericas) AS Blablabal

这个“ Blablabal”是必要的


1

对于MS Access查询,这可行:

SELECT * INTO tmpFerdeen FROM( 
    SELECT top(100) *
    FROM Customers 
UNION All 
    SELECT top(100) *  
    FROM CustomerEurope 
UNION All 
    SELECT top(100) *  
    FROM CustomerAsia 
UNION All 
    SELECT top(100) *  
    FROM CustomerAmericas
) 

这在MS Access中不起作用

SELECT top(100) * 
  INTO tmpFerdeen
  FROM Customers
UNION All
  SELECT top(100) * 
  FROM CustomerEurope
UNION All
  SELECT top(100) * 
  FROM CustomerAsia
UNION All
  SELECT top(100) * 
  FROM CustomerAmericas

1

我会这样做:

SELECT top(100)* into #tmpFerdeen
FROM Customers

Insert into #tmpFerdeen
SELECT top(100)* 
FROM CustomerEurope

Insert into #tmpFerdeen
SELECT top(100)* 
FROM CustomerAsia

Insert into #tmpFerdeen
SELECT top(100)* 
FROM CustomerAmericas

0

我在解决方案中看到的挑战:

FROM( 
SELECT top(100) *
    FROM Customers 
UNION
    SELECT top(100) *  
    FROM CustomerEurope 
UNION 
    SELECT top(100) *  
    FROM CustomerAsia 
UNION
    SELECT top(100) *  
    FROM CustomerAmericas
)

这是因为这将创建一个窗口数据集,该数据集将驻留在RAM中,而在较大的数据集上,此解决方案将产生严重的性能问题,因为它必须首先创建分区,然后将使用该分区写入临时表。

更好的解决方案如下:

SELECT top(100)* into #tmpFerdeen
FROM Customers

Insert into #tmpFerdeen
SELECT top(100)* 
FROM CustomerEurope

Insert into #tmpFerdeen
SELECT top(100)* 
FROM CustomerAsia

Insert into #tmpFerdeen
SELECT top(100)* 
FROM CustomerAmericas

选择插入到临时表中,然后添加其他行。但是,这里的缺点是数据中是否有重复的行。

最佳解决方案如下:

Insert into #tmpFerdeen
SELECT top(100)* 
FROM Customers
UNION
SELECT top(100)* 
FROM CustomerEurope
UNION
SELECT top(100)* 
FROM CustomerAsia
UNION
SELECT top(100)* 
FROM CustomerAmericas

此方法应可用于需要不同行的所有目的。但是,如果您想要重复的行,只需将UNION换成UNION ALL

祝你好运!


-1

也许试试这个?

SELECT * INTO tmpFerdeen (
SELECT top(100)* 
FROM Customers
UNION All
SELECT top(100)* 
FROM CustomerEurope
UNION All
SELECT top(100)* 
FROM CustomerAsia
UNION All
SELECT top(100)* 
FROM CustomerAmericas)

缺少必需的表别名(如果添加,别名将与接受的答案相同)
Martin Smith

-3

尝试如下操作:使用联合的结构创建最终对象表tmpFerdeen。

然后

INSERT INTO tmpFerdeen (
SELECT top(100)* 
FROM Customers
UNION All
SELECT top(100)* 
FROM CustomerEurope
UNION All
SELECT top(100)* 
FROM CustomerAsia
UNION All
SELECT top(100)* 
FROM CustomerAmericas
)

在SQL Server中,临时表是动态创建的。我想这样做而不创建最终对象表。谢谢。
Ferdeen
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.