sql server - TSQL update value with subquery -


i have 2 tables , want compare them , modify tablea (set namemod = 1) if has different rows.

to compare tables use:

 select id, name tableb  except  select id, name tablea 

and want modify tablea:

 update tablea set namemod = 1  exists (   select id, name tableb   except   select id, name tablea   ) 

but can use exists before sub-query , in case updates elements in table not different rows.

could try this:

merge tablea [target] using tableb [source]     on [target].[id] = [source].[id]     , [target].[name ] = [source].[name] when not matched target     update set namemod = 1; 

it using merge clause.

if not clause, can use cte this:

;with idsforupdate ([id]) (     select distinct id         (         select id, name tableb           except           select id, name tablea     ) ds([id], [name]) )  update tablea   set namemod = 1  tablea  inner join idsforupdate b     on a.[id] =  b.[id]; 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -