将具有现有记录和新记录的一个表中的新记录添加到SQL中的另一个表中

时间:2014-05-23 13:27:36

标签: sql ms-access

我试图将数据附加到包含到目前为止的所有数据的表中。每周我都会提取新数据(其中包含已存在于All表中的数据)并添加新记录。我在temp表中添加了一些测试数据,其中泛型,材料num等都不同但是当我运行这个查询时它仍然说它正在添加0条记录。请帮忙。

INSERT INTO ExtWafersAll ( generic, [material number], description, vendor, [net price], [std price], NumberOfDups )
    SELECT
            ExtWafersTemp.generic,
            ExtWafersTemp.[material number],
            ExtWafersTemp.description,
            ExtWafersTemp.vendor,
            ExtWafersTemp.[net price],
            ExtWafersTemp.[std price],
            ExtWafersTemp.NumberOfDups
        FROM ExtWafersTemp
        RIGHT JOIN ExtWafersAll
            ON (ExtWafersAll.NumberOfDups = ExtWafersTemp.NumberOfDups)
                AND (ExtWafersAll.[std price] = ExtWafersTemp.[std price])
                AND (ExtWafersAll.[net price] = ExtWafersTemp.[net price])
                AND (ExtWafersAll.vendor = ExtWafersTemp.vendor)
                AND (ExtWafersAll.description = ExtWafersTemp.description)
                AND (ExtWafersAll.[material number] = ExtWafersTemp.[material number])
                AND (ExtWafersAll.generic = ExtWafersTemp.generic)
        WHERE
            ExtWafersTemp.vendor <> ExtWafersAll.vendor
            OR ExtWafersTemp.description <> ExtWafersAll.description
            OR ExtWafersTemp.[material number] <> ExtWafersAll.[material number]
            OR ExtWafersTemp.generic <> ExtWafersAll.generic;

例如,在ExtWafersTemp中我们有:

Generic   Material Number     Description     Vendor     Net Price     Std Price  
j2151     sjkdga215           xxx125125       TMA        12            14   
asdg      asgasg              aggsggs         asg        15            18  

然后在ExtWafersAll:

Generic   Material Number     Description     Vendor     Net Price     Std Price  
j2151     sjkdga215           xxx125125       TMA        12            14  

我无法弄清楚如何将temp中的新记录添加到所有文件中

1 个答案:

答案 0 :(得分:0)

也许这符合您的需求:

insert into ExtWafersAll ( generic, [material number], description, vendor, [net price], [std price], NumberOfDups )
select generic, [material number], description, vendor, [net price], [std price], NumberOfDups
from ExtWafersTemp
except
select generic, [material number], description, vendor, [net price], [std price], NumberOfDups
from ExtWafersAll;

在上面的代码片段中,您可以从ExtWafersTemp表中添加ExtWafersAll表中不存在的记录。这是你想要实现的目标吗?

关于&#34;除了&#34;你可以在这里阅读的运营商:http://en.wikipedia.org/wiki/Set_operations_%28SQL%29

更新

因为它曾经是MS Access问题,你可以尝试测试一下:

SELECT
  ExtWafersTemp.generic,
  ExtWafersTemp.[material number],
  ExtWafersTemp.description,
  ExtWafersTemp.vendor,
  ExtWafersTemp.[net price],
  ExtWafersTemp.[std price],
  ExtWafersTemp.NumberOfDups
FROM ExtWafersAll RIGHT JOIN ExtWafersTemp 
ON (ExtWafersAll.NumberOfDups = ExtWafersTemp.NumberOfDups
    AND ExtWafersAll.[std price] = ExtWafersTemp.[std price]
    AND ExtWafersAll.[net price] = ExtWafersTemp.[net price]
    AND ExtWafersAll.vendor = ExtWafersTemp.vendor
    AND ExtWafersAll.description = ExtWafersTemp.description
    AND ExtWafersAll.[material number] = ExtWafersTemp.[material number]
    AND ExtWafersAll.generic = ExtWafersTemp.generic)
WHERE ExtWafersAll.NumberOfDups is null
  AND ExtWafersAll.[std price] is null
  AND ExtWafersAll.[net price] is null
  AND ExtWafersAll.vendor is null
  AND ExtWafersAll.description is null
  AND ExtWafersAll.[material number] is null
  AND ExtWafersAll.generic is null

Genarally它是一个以下模式(例如,有一个主键字段 - id):

select tt.id
from tableall t right join tabletemp tt 
  on (t.id = tt.id)
where t.id is null

希望它有所帮助。