C# 比较两个表是否匹配和更新或移动和删除行
如果两个表中都有匹配的值,我需要比较两个表。 表的结构C# 比较两个表是否匹配和更新或移动和删除行,c#,sql,duplicates,matching,C#,Sql,Duplicates,Matching,如果两个表中都有匹配的值,我需要比较两个表。 表的结构 Persons ID | fname | lname | address | socialNumber | taxNumber | Match Personals ID | fname | lname | address | socialNumber | taxNumber 我有两个参数正在检查是否匹配,socialNumber和/或taxNumber 个人需要与个人进行比较 如果找到匹配项,则使用个人ID(匹配项)更新ID(以个人为单
Persons
ID | fname | lname | address | socialNumber | taxNumber | Match
Personals
ID | fname | lname | address | socialNumber | taxNumber
我有两个参数正在检查是否匹配,socialNumber和/或taxNumber
UPDATE Persons SET Match = 1, ID = (SELECT MAX(ID) FROM Personals) WHERE taxNumber IN ( SELECT taxNumber FROM Personals GROUP BY taxNumber HAVING ( COUNT(taxNumber) > 1 ))
我认为在一个查询中很难做到这一点,不使用游标也是如此,但您可以将操作分成如下两部分:
--update action
update a
set a.id = b.id, a.match =1
from _persons a inner join _personals b
on a.social = b.social or a.taxnumber = b.taxnumber
--delete / insert action
declare @RowCount as integer
select @RowCount = count(a.id) from _persons a
where ID not in (
select a.id
from _persons a inner join _personals b
on a.social = b.social or a.taxnumber = b.taxnumber
)
if @RowCount>0
begin
insert into _Personals (ID, fname, lname, address, social, taxnumber)
select ID, fname, lname, address, social, taxnumber from _persons a
where ID not in (
select a.id
from _persons a inner join _personals b
on a.social = b.social or a.taxnumber = b.taxnumber
)
delete _persons
where ID not in
(
select a.id
from _persons a inner join _personals b
on a.social = b.social or a.taxnumber = b.taxnumber
)
end