读书人

datatable 相减解决办法

发布时间: 2012-03-12 12:45:33 作者: rapoo

datatable 相减
请问,我现在有两个结构一样的datatable,我想按照主键把不一样的row去掉,如何操作?

[解决办法]
delete tb1 where not exists(select * from tb2 where tb2.key = tb1.key)
[解决办法]
主的意思是在DataTable象的例中行操作?
[解决办法]
--例子--

create table tb1(id int,c int)
insert tb1
select 1,1
union select 2,1
union select 3,1

create table tb2(id int,c int)
insert tb2
select 1,1
union select 5,1
union select 6,1

--先删除tb1中不在tb2中的记录
delete a
from tb1 a left join tb2 b on a.id = b.id
where b.id is null

--再先删除tb2中不在tb1中的记录
delete a
from tb2 a left join tb1 b on a.id = b.id
where b.id is null

select * from tb1
select * from tb2


drop table tb1,tb2

读书人网 >SQL Server

热点推荐