insert into Book ( ISBN , Price , Shelf , archivedAt )
select ISBN , Price , Shelf , getdate() from
( merge book as b
using WeeklyChange as wc
on b.ISBN=wc.ISBN and b.archivedAt is null
when matched and ( b.price<>wc.price or b.shelf<>wc.shelf ) then
update set b.price=wc.price , b.shelf=wc.shelf
when not matched then
insert values(wc.ISBN , wc.price , wc.shelf)
output $action , wc.ISBN , Deleted.Price , Deleted.Shelf
) changes( action , ISBN , Price , Shelf)
where action='update' ;