Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Using UPDATE with MERGE When Data Hasn't Changed

$
0
0

In my MERGE statement, my source and target are mapped on the BenefitLookupGroup1Key (see query below). If that value matches between the source and target tables, an UPDATE between the two tables occurs on all fields.

What I would like to do is further qualify when an UPDATE occurs by determining whether or not there has been a change on the business values. That is, if target.BenefitLookupGroup1Key = source.BenefitLookupGroup1Key, but one or more target/source columns in the UPDATE have not changed, I don't want the UPDATE to occur.

Is this possible to do? The UPDATE portion of the MERGE query:

--**************

merge MasterODS.Member.BenefitLookupGroup1_destination as target 
using
(select BenefitLookupGroup1Key, InsurerKey, MemberPlanNumber, MemberPlanNumberDescription, MemberGroupNumber, MemberGroupNumberDescription, MemberEmployeeIdentificationNumber, MemberEmployeeIdentificationNumberDescription, MemberInsurancePolicyNumber, MemberInsurancePolicyNumberDescription, MemberNetworkID, MemberNetworkIDDescription, BenefitInformation, CoverageLevelCode, ServiceTypeCode, InsuranceTypeCode, PlanCoverageDescription, RowEffectiveDate, RowEndDate, AuditRowDate, AuditRowProcessName, AuditRowUserName from MSIWStaging.member.benefitlookupgroup1_source where IsProcessedInd = @processing
) as source on target.BenefitLookupGroup1Key = Source.BenefitLookupGroup1Key

--When records match, update.
When matched
then update
set 
--target.BenefitLookupGroup1Key=source.BenefitLookupGroup1Key
target.InsurerKey=source.InsurerKey
, target.MemberPlanNumber=source.MemberPlanNumber
, target.MemberPlanNumberDescription=source.MemberPlanNumberDescription
, target.MemberGroupNumber=source.MemberGroupNumber
, target.MemberGroupNumberDescription=source.MemberGroupNumberDescription
, target.MemberEmployeeIdentificationNumber =source.MemberEmployeeIdentificationNumber
, target.MemberEmployeeIdentificationNumberDescription=source.MemberEmployeeIdentificationNumberDescription
, target.MemberInsurancePolicyNumber=source.MemberInsurancePolicyNumber
, target.MemberInsurancePolicyNumberDescription =source.MemberInsurancePolicyNumberDescription
, target.MemberNetworkID        =source.MemberNetworkID
, target.MemberNetworkIDDescription=source.MemberNetworkIDDescription
, target.BenefitInformation=source.BenefitInformation
, target.CoverageLevelCode=source.CoverageLevelCode
, target.ServiceTypeCode=source.ServiceTypeCode
, target.InsuranceTypeCode=source.InsuranceTypeCode
, target.PlanCoverageDescription=source.PlanCoverageDescription
, target.RowEffectiveDate=source.RowEffectiveDate--revisit later (mod6).
, target.RowEndDate=source.RowEndDate--revisit later.
, target.AuditRowDate=GetDate()--use getdate(), not a variable.
, target.AuditRowProcessName=@ProcedureName
, target.AuditRowUserName=@SuserSname

--***********************

Thank you for your help.

cdun2


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>