SQL Server 2008 Merge Statement Multiple Match Conditions -
i attempting bring in claim data in incremental manner. matching source , target on system (varchar) , claimnum (varchar), hash of other columns check changes.
i have merge statement (simplified):
merge target using source on target.system = source.system , target.claimnum = source.claimnum when matched , target.hashvalue <> source.hashvalue {update claim record data} when matched , target.hashvalue = source.hashvalue {update claim record "checked"} when not matched {insert new claim record} however, can't have 2 matched conditions. how else can accomplish merge statement?
this being used on sql server 2008.
you use case or iif(sql server 2012) add custom logic:
merge target using source on target.system = source.system , target.claimnum = source.claimnum when matched update set column_name = case when target.hashvalue = source.hashvalue ... else ... end when not matched {insert new claim record} edit:
if update use multiple conditions/set same value skip:
... update set column_name1 = case when condition1 ... else column_name1 end ,column_name2 = case when condition1 ... when condition2 ... else column_name2 end ,... note:
if source/target hashvalue nullable should handle using coalesce/isnull/adding or target.hashvalue null.... comparison operators don't work null.
Comments
Post a Comment