在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子句。
答案 0 :(得分:185)
这适用于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 :(得分:118)
您根本不需要派生表。
只需将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”是必要的
答案 3 :(得分: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
答案 4 :(得分: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
答案 5 :(得分: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
祝你好运!
答案 6 :(得分:-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)
答案 7 :(得分:-3)
尝试这样的事情:使用union的结构创建最终的对象表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
)