2009-02-26 84 views
103

在SQL Server此插入100條記錄,從Customers表到tmpFerdeen: -是否可以在UNION [ALL]中使用SELECT INTO子句?

SELECT top(100)* 
INTO tmpFerdeen 
FROM Customers 

是否有可能跨越UNION做一個SELECT INTO ALL SELECT: -

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子句。

+0

確定你需要工會嗎? – sfossen 2009-02-26 16:52:52

+0

是的。由於記錄在所有表格中都是唯一的。 – Ferdeen 2009-02-26 17:02:11

回答

147

此作品在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 top 100 * INTO tmpFerdeen FROM Customers UNION所有SELECT top 100 * FROM CustomerEurope UNION所有SELECT top 100 * FROM CustomerAsia UNION所有SELECT前100 *從CustomerAmericas(對不起,不能格式化這裏的SQL)。謝謝! – Ferdeen 2009-02-26 17:06:20

-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 
) 
+0

在SQL Server中臨時表是即時創建的。我想這樣做而不創建最終的對象表。謝謝。 – Ferdeen 2009-02-26 16:56:24

-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) 
+0

缺少所需的表別名(如果添加將與接受的答案相同) – 2012-09-27 10:30:25

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」 是必要的

90

你不需要派生表在所有的這一點。

只要把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 
0

對於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 
相關問題