Thursday, January 21, 2010

Merge tables Sample

select * from [target_stage]

Merge into [target] as t
using target_stage s on t.lineid = s.lineid

when matched then update
set t.fname = s.fname ,
t.lname = s.lname,
t.[ModifiedTimeStamp] = s.[ModifiedTimeStamp]

when not matched then insert ([LineID],[Fname],[Lname],[InsertDate],[ModifiedTimeStamp])
values (s.[LineID],s.[Fname],s.[Lname],s.[InsertDate],s.[ModifiedTimeStamp])

when not matched by source then delete

output INSERTED.Lineid;

select * from [target]

No comments: