by Mikael Henriksson
5. May 2010 13:07
-- select table to merge into
merge into dbo.post p
-- select table to merge from
using (select tag from template_tag) tt
-- select how they map together
on p.tag = tt.tag and p.post_id = @post_id
-- specify WHEN to do
when not matched then
-- specify WHAT to do
insert (tag, post_id)
values (tt.tag, @post_id)
-- specify a second WHEN
when matched then
-- specify and action to perform
update set tag = tt.tag where post_id = @post_id;
A very simple example on the greatness of merge. Now you can quite easily work on those sets in a more natural way. Mostly for my self this post is.