日期:2014-05-16  浏览次数:20376 次

怎么比较两个或者以上的字段?
有个表,里面有两个字段:ID 和DetailName。ID是uniqueidentifier型,DetailName是字符型。两个加起来可以才定义一种产品。

我现在要把一批新的产品加到这个表里。要求是,如果原来表里已经有了,那么就不加,没有 就加。

如果用oldT.ID<>newT.ID and oldT.DetailName<>newT.DetailName 这样的条件,那么同ID不同DetailName的商品就会被遗漏。
------解决方案--------------------
insert into tb
select *
from newtb a
where not exists (select 1 from tb b where a.id = b.id and a.name = b.name)
------解决方案--------------------
insert into newt as a where not exists(select 1 from oldt where ID=a.ID and DetailName<>a.DetailName)