如果我理解正确(实际上显示一些测试数据和预期结果会有很大帮助),您可以使用一系列EXISTS
表达式或一系列外连接。这是一个使用 2 个不同列的示例,每个列都有一个相应的“主表”:
-- set up test data
declare @RawData table (Location varchar(100) not null primary key, Country varchar(100) not null, Status char(4) null)
declare @LocationMaster table (Location varchar(100) not null primary key)
declare @CountryMaster table (Country varchar(100) not null primary key)
insert into @RawData (Location, Country) values ('New Jersey', 'USA'), ('New Jarsey', 'USA'), ('New York', 'USAA'), ('New Yoik', 'United States')
insert into @LocationMaster (Location) values ('New Jersey'), ('New York')
insert into @CountryMaster (Country) values ('USA')
-- option 1: EXISTS
update @RawData
set Status = 'SKIP'
from @RawData r
where
not exists (select * from @LocationMaster where Location = r.Location)
or not exists (select * from @CountryMaster where Country = r.Country)
select * from @RawData
-- reset status
update @RawData set Status = null
-- option 2: OUTER JOIN
update @RawData
set Status = 'SKIP'
from
@RawData r
left outer join @LocationMaster lm on lm.Location = r.Location
left outer join @CountryMaster cm on cm.Country = r.Country
where
lm.Location is null
or cm.Country is null
select * from @RawData
您可以在 SSMS 中分析这两个计划,以确定哪一个对您的数据集更有效。