使用where子句将两个表中的数据放入一个表中

时间:2017-01-15 13:40:37

标签: sql sql-server database

我有两个文件,我已批量导入两个具有重叠日期的表。现在我想将两个表中的数据移动到一个新表中,但删除重叠日期。这是我的代码:

-- Pull across data from P1-10 and exclude out of scope dates
SELECT      *
INTO        I_Inventory
FROM        I_Inventory_P10_Temp
WHERE       [date] >= '01/01/2016' and [date] <= '10/31/16'

-- Pull across data from P11-12 and exclude out of scope dates
SELECT      *
INTO        I_Inventory
FROM        I_Inventory_P12_Temp
WHERE       [date] >= '11/01/2016' and [date] <= '12/31/16'

然而,这并不起作用,因为它表示在第一次选择之后表已经存在。有人可以让我知道如何处理这个问题吗?我对SQL很陌生。

1 个答案:

答案 0 :(得分:1)

使用UNION ALL组合结果,然后插入表格

SELECT      *
INTO        I_Inventory
FROM        I_Inventory_P10_Temp
WHERE       [date] >= '01/01/2016' and [date] <= '10/31/16'
Union All
-- Pull across data from P11-12 and exclude out of scope dates
SELECT      *
FROM        I_Inventory_P12_Temp
WHERE       [date] >= '11/01/2016' and [date] <= '12/31/16'