sql server - Efficient way to match column values with corresponding master table values -


i importing excel data in stored procedure , storing records in temporary table. want validate values of few columns corresponding values in master table.

i have added 1 more column temp table namely: status, holds either null or skip value.

for instance, temporary table contains 1 column namely location. customers send pre-filled excel sheet columns filled. on such column location column. spelling of location not correct. if location say, new jersey, excel sheet might contain spelling new jarsey.

i have location master table stores correct names of locations , ids well. want match location name in temp table corresponding location name in master table. if location fails match, mark status column skip in temp table.

there several columns in temp table values of needs match corresponding master table values.

is there way verify these column values in more efficient , faster way? want match locations row row , other column values.

if understand correctly (actually showing test data , expected results lot), use either series of exists expressions or series of outer joins. here's example using 2 different columns, each corresponding 'master table':

-- set 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 @rawdata (location, country) values ('new jersey', 'usa'), ('new jarsey', 'usa'), ('new york', 'usaa'), ('new yoik', 'united states') insert @locationmaster (location) values ('new jersey'), ('new york') insert @countrymaster (country) values ('usa')  -- option 1: exists  update @rawdata set status = 'skip' @rawdata r      not exists (select * @locationmaster location = r.location)     or not exists (select * @countrymaster country = r.country)  select * @rawdata  -- reset status update @rawdata set status = null  -- option 2: outer join  update @rawdata set status = 'skip'      @rawdata r     left outer join @locationmaster lm on lm.location = r.location     left outer join @countrymaster cm on cm.country = r.country     lm.location null      or cm.country null  select * @rawdata 

you can profile both plans in ssms determine 1 more efficient data set.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -