2013-05-14 33 views
0

我正在導入存儲過程中的Excel數據並將記錄存儲在臨時表中。我想用主表中的相應值驗證少數列的值。將列值與相應主表值匹配的有效方法

我已經爲此臨時表添加了一列,即:Status,其中包含NULL或SKIP作爲值。

例如,臨時表包含一列即位置。客戶向我們發送預填充的Excel表格,並填寫所有欄目。在這個列是這個位置列。通常拼寫的位置不正確。如果有任何位置說新澤西州,Excel工作表可能包含新Jarsey的拼寫。

我有一個位置主表,其中存儲位置和ID的正確名稱以及。我想要將臨時表中的位置名稱與主表中相應的位置名稱進行匹配。如果位置不匹配,我將狀態列標記爲臨時表中的SKIP。

臨時表中有幾列需要與其對應的主表值匹配。

有什麼辦法以更高效,更快速的方式驗證這些列值?我想要按行匹配位置以及其他列值。

+0

描述您的真實業務需求,**以及**您在解決方案中的失敗嘗試。 –

回答

0

如果我理解正確(實際上顯示一些測試數據和預期結果會有很大幫助),則可以使用一系列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兩個計劃,以確定哪一個是你的數據集更高效。

相關問題