Link to home
Start Free TrialLog in
Avatar of Paula DiTallo
Paula DiTalloFlag for United States of America

asked on

MERGE in SQL Server

Techies--

Will someone explain to me why this MERGE statement is setting the RecordIsDeleted flag to 1 on every row? Obviously NOT MATCHED by SOURCE trips it-- but I thought the criteria for the MATCH is on the 2 keys under the ON condition--not every column after the ON, which is what it appears to be doing.  Any advice on how to correct this?



MERGE dimension.CustomerSegment  AS TARGET

	-- SELECT ALL COLUMNS IN THE SOURCE ETL TABLE
	USING ( SELECT DS.SegmentKey
				 , DC.CustomerKey
				 , DCS.[MemberInternalKey]
				 , DCS.[BuyingUnitInternalKey]
				 , DCS.[SegmentInternalKey]
				 , DCS.[MemberDK]
				 , DCS.[BuyingUnitDK]
				 , DCS.[SegmentId]
				 , DCS.[SegmentDescription]
				 , DCS.[MatrixMemberId]
				 , DCS.[ClubInternalKey]
				 , DCS.[UpdatedDate]
				 , DCS.[StartDate]
				 , DCS.[EndDate]
				 , DCS.[AttachmentSourceId]
				 , DCS.[AttachmentSourceName] 
				 , DCS.[RecordFromDate]
				 , DCS.[RecordToDate]
				 , DCS.[RecordIsCurrent]
				 , DCS.[RecordisDeleted]        
			  FROM [etl].[CustomerSegment]		DCS
			 INNER JOIN dimension.Customer		 DC		ON DC.MemberDK				=	DCS.MemberDK
													   AND DC.BuyingUnitDK			=	DCS.BuyingUnitDK
			 INNER JOIN dimension.Segment		 DS		ON DS.SegmentInternalKey	=	DCS.SegmentInternalKey

          ) AS Source
                 ( SegmentKey
				 , CustomerKey
				 , [MemberInternalKey]
				 , [BuyingUnitInternalKey]
				 , [SegmentInternalKey]
				 , [MemberDK]
				 , [BuyingUnitDK]
				 , [SegmentId]
				 , [SegmentDescription]
				 , [MatrixMemberId]
				 , [ClubInternalKey]
				 , [UpdatedDate]
				 , [StartDate]
				 , [EndDate]
				 , [AttachmentSourceId]
				 , [AttachmentSourceName]
				 , [RecordFromDate]
				 , [RecordToDate]
				 , [RecordIsCurrent]
				 , [RecordisDeleted]
				 )
		ON (    Source.CustomerKey		= Target.CustomerKey		-- Key will be customer, segment, and segment start date.	
		         AND Source.SegmentKey	= Target.SegmentKey			-- Remember that segments can come and go
			
	

		   )

		-- NO MATCH IN TARGET MEANS NEW DATA - PERFORM AN INSERT
		WHEN NOT MATCHED BY TARGET THEN
	    INSERT (   [SegmentKey]
				  ,[CustomerKey]
				  ,[UpdatedDate]
				  ,[StartDate]
				  ,[EndDate]
				  ,[AttachmentSourceId]
				  ,[AttachmentSource]
				  ,[RecordFromDate]
				  ,[RecordToDate]
				  ,[RecordIsCurrent]
				  ,[RecordisDeleted]
				  ,[InsertedBy]
				  ,[InsertedDate]
				  ,[ModifiedBy]
				  ,[ModifiedDate]
		      )
        VALUES
             (   Source.[SegmentKey]
				,Source.[CustomerKey]
				,Source.[UpdatedDate]
				,Source.[StartDate]
				,Source.[EndDate]
				,Source.[AttachmentSourceId]
				,Source.[AttachmentSourceName]
				,Source.[RecordFromDate]
				,Source.[RecordToDate]
				,Source.[RecordIsCurrent]
				,Source.[RecordisDeleted]
			    ,@ExecutedBy						-- InsertedBy
			    ,GetDate()						    -- InsertedDate
			    ,@ExecutedBy						-- ModifiedBy
			    ,GetDate()						    -- ModifiedDate
		     )
		-- NO MATCH IN SOURCE MEANS WHAT SEGMENT DID EXIST FOR MEMBER IS NO LONGER CURRENT
		-- *Note: Because deleting/fragmenting indexes can be a performance killer, the row wil
		--        be flagged as RecordIsDeleted to TRUE.

		WHEN NOT MATCHED BY SOURCE THEN
		 UPDATE	
		   SET RecordIsDeleted		=	1   -- Set for future delete/reindex maint job.

		WHEN MATCHED			
         THEN UPDATE	
			SET Updateddate				=	Source.[Updateddate]		
			,StartDate					=	Source.[StartDate]			
			,EndDate					=	Source.[EndDate]
			,AttachmentSourceId			=	Source.[AttachmentSourceId]
			,AttachmentSource			=	Source.[AttachmentSourceName]
			,RecordToDate				=	Source.[RecordToDate]
			,[RecordIsCurrent]			=	Source.[RecordIsCurrent]
			,[RecordIsDeleted]			=	Source.[RecordIsDeleted]  
			,ModifiedBy					=	@ExecutedBy						-- ModifiedBy
			,ModifiedDate				=	GetDate()						--  ModifiedDate	


 ; -- END OF THE MERGE STATEMENT

Open in new window

Avatar of Paula DiTallo
Paula DiTallo
Flag of United States of America image

ASKER

@hnasr, there isn't an odbc component to this particular issue.
Avatar of Hamed Nasr
Unable to reproduce the issue.

Can you help me in reproducing the tables, so to understand and test the problem
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Maddening isn't it? This is a perfectly good merge statement that should work--but it just plain doesn't with the live data--I've had to give up on this approach and go the update/insert/delete route... due to time constraints.  Thanks for looking at this.
My hunch still revolves around a data issue.  I'll be glad to keep working with you if you want to dig some more.
Thanks Kdo--now that the immediate need to solve this has past, I'll resurrect the what I have and post a test script