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
Post a Comment