﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Reporting Services / Reporting Services  / MERGE problem -&amp;gt; when I need to reenter an entry which is already in the history / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sun, 19 May 2013 14:59:08 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: MERGE problem -&amp;gt; when I need to reenter an entry which is already in the history</title><link>http://www.sqlservercentral.com/Forums/Topic1426286-150-1.aspx</link><description>Thank you! :-)</description><pubDate>Tue, 05 Mar 2013 09:00:51 GMT</pubDate><dc:creator>chingarova</dc:creator></item><item><title>RE: MERGE problem -&amp;gt; when I need to reenter an entry which is already in the history</title><link>http://www.sqlservercentral.com/Forums/Topic1426286-150-1.aspx</link><description>[quote][b]chingarova (3/5/2013)[/b][hr]ok, lets say that the script look like this:[/quote]It's an improvement in that you now have a "key" (not ideal - not everybody has an email address, many folks have several) to identify individuals, but in terms of the overall functionality of the merge statement, it changes little. I honestly think it would be cheaper for you to start again from scratch, laying out the business rules first. There are a number of possible cases when you compare the two tables.1. Match, Target.IsRowCurrent = 12. Match, Target.IsRowCurrent = 03. Person in target but not source (WHEN NOT MATCHED BY SOURCE), Target.IsRowCurrent = 14. Person in target but not source, Target.IsRowCurrent = 05. Person in source but not targetDefine [i]fully [/i]what you want to do for each case.</description><pubDate>Tue, 05 Mar 2013 06:18:21 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: MERGE problem -&amp;gt; when I need to reenter an entry which is already in the history</title><link>http://www.sqlservercentral.com/Forums/Topic1426286-150-1.aspx</link><description>ok, lets say that the script look like this:USE masterGO--Create the main Persons tableCREATE TABLE Persons   ( FirstName varchar(20),LastName varchar (20),Title varchar(10),Email varchar (20),IsRowCurrent tinyint,LastUpdated DATETIME,ValidFrom DATETIME,ValidTo DATETIME)--Populate Persons table with existing customersINSERT INTO PersonsVALUES ('Anna','Kournikova','Miss','anna@emailme.com',1,'2012-11-01','2012-11-01','9999-12-31')       ,('Roger','Federer','Mr','roger@emailme.com',1,'2012-11-01','2012-11-01','9999-12-31')GO--Create an update table to hold new/updated personsCREATE TABLE NewPersons(  FirstName varchar (20) ,LastName varchar (20) ,Title varchar(10) ,Email varchar (20))--Populate People table with existing peopleINSERT INTO NewPersons VALUES  ('Anna','Smith','Mrs','anna@emailme.com') --Update       ,('Rafael','Nadal','Mr','roger@emailme.com') --New entryGO----------------------------------------------------------------------------- Mixed Merge - Type 1 and 2INSERT INTO Persons   ( FirstName    ,LastName    ,Title    ,Email    ,IsRowCurrent    ,LastUpdated    ,ValidFrom    ,ValidTo   ) SELECT     FirstName    ,LastName    ,Title    ,Email    ,1            --IsRowCurrent    ,GETDATE()    --LastUpdated    ,GETDATE()    --ValidFrom    ,'9999-12-31' --ValidToFROM (	MERGE INTO Persons AS [Target]	USING NewPersons AS [Source]	ON Target.Email = Source.Email       WHEN MATCHED and Target.IsRowCurrent = 1 -- SCD 1/2 changes    AND      (         Target.FirstName &amp;lt;&amp;gt; Source.LastName      OR Target.LastName &amp;lt;&amp;gt; Source.LastName      OR Target.Title &amp;lt;&amp;gt; Source.Title       )     THEN UPDATE SET              Target.IsRowCurrent = 0 --Obsolete       ,Target.LastUpdated = GETDATE()       ,Target.ValidTo   = GETDATE()       WHEN NOT MATCHED -- New entries      THEN INSERT (        FirstName       ,LastName       ,Title       ,Email       ,IsRowCurrent       ,LastUpdated       ,ValidFrom       ,ValidTo       )       Values (        Source.FirstName       ,Source.LastName       ,Source.Title       ,Source.Email       ,1            --IsRowCurrent       ,GETDATE()    --LastUpdated       ,GETDATE()    --ValidFrom       ,'9999-12-31' --ValidTo       )    WHEN NOT MATCHED BY SOURCE  -- Obsolete removed entries    AND Target.IsRowCurrent = 1      THEN UPDATE SET         Target.IsRowCurrent = 0  --Removed         ,Target.LastUpdated  = GETDATE()         ,Target.ValidTo      = GETDATE()       OUTPUT  $action AS Action        ,[Source].*) AS MergeOutput      WHERE MergeOutput.Action = 'UPDATE'      AND FirstName IS NOT NULL;</description><pubDate>Tue, 05 Mar 2013 05:57:18 GMT</pubDate><dc:creator>chingarova</dc:creator></item><item><title>RE: MERGE problem -&amp;gt; when I need to reenter an entry which is already in the history</title><link>http://www.sqlservercentral.com/Forums/Topic1426286-150-1.aspx</link><description>[quote][b]chingarova (3/5/2013)[/b][hr]OK, let me explain...The script I posted is just an example(my real example is different and much more complex,but this doesn't matter), what I need to understand is why when we have a MERGE STATEMENT WHICH KEEPS HISTORY, WHEN AN ENTRY IS OBSOLETE AND THEN IT IS ADDED AGAIN IN THE UPDATE TABLE, IT IS NOT UPDATED IN THE MAIN TABLE AND IT IS STILL SHOWN AS OBSOLETE....if you have suggestion regarding this, it will helps. Thanks[/quote]Please don't shout.Every successful update [code="sql"]WHEN MATCHED and Target.IsRowCurrent = 1 -- SCD 1/2 changes AND ( Target.FirstName &amp;lt;&amp;gt; Source.FirstName OR Target.LastName &amp;lt;&amp;gt; Source.LastName OR Target.Title &amp;lt;&amp;gt; Source.Title ) THEN UPDATE SET [/code]will cause a duplicate row (duplicate on firstname) to be inserted into the table, via the OUTPUT clause.There are way too many serious logic flaws in the posted example script for it to be of any use. Can you post the actual merge statement you are using?</description><pubDate>Tue, 05 Mar 2013 03:05:06 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: MERGE problem -&amp;gt; when I need to reenter an entry which is already in the history</title><link>http://www.sqlservercentral.com/Forums/Topic1426286-150-1.aspx</link><description>OK, let me explain...The script I posted is just an example(my real example is different and much more complex,but this doesn't matter), what I need to understand is why when we have a MERGE STATEMENT WHICH KEEPS HISTORY, WHEN AN ENTRY IS OBSOLETE AND THEN IT IS ADDED AGAIN IN THE UPDATE TABLE, IT IS NOT UPDATED IN THE MAIN TABLE AND IT IS STILL SHOWN AS OBSOLETE....if you have suggestion regarding this, it will helps. Thanks</description><pubDate>Tue, 05 Mar 2013 02:54:09 GMT</pubDate><dc:creator>chingarova</dc:creator></item><item><title>RE: MERGE problem -&amp;gt; when I need to reenter an entry which is already in the history</title><link>http://www.sqlservercentral.com/Forums/Topic1426286-150-1.aspx</link><description>[quote][b]chingarova (3/5/2013)[/b][hr]The business key is the FirstName. I am merging the initial table with update tables and I am doing this based on the FirstName(which will not chanege in my case, only the LastName may change -for example if Anna gets married). But I don't think this is the focus of my question...Thanks[/quote]So long as there aren't more than half a dozen or so employees in the table, that will be just Jim Dandy. A few years ago I worked for a small software house with 26 people on site, of whom 7 were called Chris (I lie - one of them was Kris). It really [i]isn't[/i] going to work, is it? Accounting for an edge case - surname change on marriage - is completely screwing up your logic. Take the edge case out of this equation entirely, deal with them with separate code. All you need is an update!Back to the majority. Use Firstname &amp; Lastname as business key, it will make your merge logic much simpler.[code="sql"]--Create an update table to hold new/updated persons[/code]The newpersons table would have to contain all retained persons in addition to changes, otherwise WHEN NOT MATCHED BY SOURCE clause will mark them as obsolete. [code="sql"]WHEN MATCHED and Target.IsRowCurrent = 1 -- SCD 1/2 changes AND ( Target.FirstName &amp;lt;&amp;gt; Source.FirstName[/code]Why? They're matched on Firstname.Personally, I'd throw this merge statement away and start again - after first rewriting the business rules. </description><pubDate>Tue, 05 Mar 2013 02:47:00 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: MERGE problem -&amp;gt; when I need to reenter an entry which is already in the history</title><link>http://www.sqlservercentral.com/Forums/Topic1426286-150-1.aspx</link><description>The business key is the FirstName. I am merging the initial table with update tables and I am doing this based on the FirstName(which will not chanege in my case, only the LastName may change -for example if Anna gets married). But I don't think this is the focus of my question...Thanks</description><pubDate>Tue, 05 Mar 2013 02:13:04 GMT</pubDate><dc:creator>chingarova</dc:creator></item><item><title>RE: MERGE problem -&amp;gt; when I need to reenter an entry which is already in the history</title><link>http://www.sqlservercentral.com/Forums/Topic1426286-150-1.aspx</link><description>[quote][b]chingarova (3/4/2013)[/b][hr]Hi,I am having a BIG problem with the merge statement in SQL 2008: When I have an entry (for example an employee name) in the table which is already old and saved in the history -&amp;gt;if it is added again it appears in the results as old..... For example if John Smith is part of the company then leaves (it is saved in the history as an obsolete entry) and then again comes back to the company in the result sections John is still obsolete...???!!!Help please!!![/quote]Your biggest problem is this:[code="sql"]	MERGE INTO Persons AS [Target]	USING NewPersons AS [Source]	ON Target.FirstName = Source.FirstName[/code]Which John or Anna is matched?</description><pubDate>Tue, 05 Mar 2013 02:00:20 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: MERGE problem -&amp;gt; when I need to reenter an entry which is already in the history</title><link>http://www.sqlservercentral.com/Forums/Topic1426286-150-1.aspx</link><description>Here the scriptCREATE TABLE Persons   ( ID int IDENTITY PRIMARY KEY,FirstName varchar(20),LastName varchar (20),Title varchar(10),IsRowCurrent tinyint,LastUpdated DATE,ValidFrom DATE,ValidTo DATE)--Populate Persons table with existing customersINSERT INTO PersonsVALUES ('Anna','Kournikova','Miss',1,'2012-11-01','2012-11-01','9999-12-31')       ,('Roger','Federer','Mr',1,'2012-11-01','2012-11-01','9999-12-31')GO--Create an update table to hold new/updated personsCREATE TABLE NewPersons( FirstName varchar (20),LastName varchar (20),Title varchar(10))--Populate People table with existing peopleINSERT INTO NewPersons VALUES ('Anna','Smith','Mrs') --Update       ,('Rafael','Nadal','Mr') --New entryGO----------------------------------------------------------------------------- Mixed Merge - Type 1 and 2INSERT INTO Persons   ( FirstName    ,LastName    ,Title    ,IsRowCurrent    ,LastUpdated    ,ValidFrom    ,ValidTo   ) SELECT     FirstName    ,LastName    ,Title    ,1            --IsRowCurrent    ,GETDATE()    --LastUpdated    ,GETDATE()    --ValidFrom    ,'9999-12-31' --ValidToFROM (	MERGE INTO Persons AS [Target]	USING NewPersons AS [Source]	ON Target.FirstName = Source.FirstName	         WHEN MATCHED and Target.IsRowCurrent = 1 -- SCD 1/2 changes    AND      (	    Target.FirstName &amp;lt;&amp;gt; Source.FirstName	 OR Target.LastName &amp;lt;&amp;gt; Source.LastName	 OR Target.Title &amp;lt;&amp;gt; Source.Title       )     THEN UPDATE SET              Target.IsRowCurrent = 0 --Obsolete       ,Target.LastUpdated = GETDATE()       ,Target.ValidTo   = GETDATE()       WHEN NOT MATCHED -- New entries      THEN INSERT (        FirstName       ,LastName       ,Title       ,IsRowCurrent       ,LastUpdated       ,ValidFrom       ,ValidTo       )       Values (        Source.FirstName       ,Source.LastName       ,Source.Title       ,1            --IsRowCurrent       ,GETDATE()    --LastUpdated       ,GETDATE()    --ValidFrom       ,'9999-12-31' --ValidTo       )    WHEN NOT MATCHED BY SOURCE  -- Obsolete removed entries    AND Target.IsRowCurrent = 1      THEN UPDATE SET         Target.IsRowCurrent = 0  --Removed         ,Target.LastUpdated  = GETDATE()         ,Target.ValidTo      = GETDATE()       OUTPUT  $action AS Action        ,[Source].*) AS MergeOutput      WHERE MergeOutput.Action = 'UPDATE'      AND FirstName IS NOT NULL;If Anna is removed and afterwards she is entyered again, she still appears as unactive (IsRowCurrent=0)...</description><pubDate>Tue, 05 Mar 2013 01:13:38 GMT</pubDate><dc:creator>chingarova</dc:creator></item><item><title>RE: MERGE problem -&amp;gt; when I need to reenter an entry which is already in the history</title><link>http://www.sqlservercentral.com/Forums/Topic1426286-150-1.aspx</link><description>Hi and welcome to SSC. You seem to have a challenge you are dealing with. Unfortunately you haven't posted anywhere near enough information for anybody to be able to provide much help. Please take a few minutes to read the article in my signature about best practices when posting questions.</description><pubDate>Mon, 04 Mar 2013 09:38:45 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>MERGE problem -&amp;gt; when I need to reenter an entry which is already in the history</title><link>http://www.sqlservercentral.com/Forums/Topic1426286-150-1.aspx</link><description>Hi,I am having a BIG problem with the merge statement in SQL 2008: When I have an entry (for example an employee name) in the table which is already old and saved in the history -&amp;gt;if it is added again it appears in the results as old..... For example if John Smith is part of the company then leaves (it is saved in the history as an obsolete entry) and then again comes back to the company in the result sections John is still obsolete...???!!!Help please!!!</description><pubDate>Mon, 04 Mar 2013 08:42:06 GMT</pubDate><dc:creator>chingarova</dc:creator></item></channel></rss>