﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Discuss Content Posted by Vincent Rainardi / Article Discussions / Article Discussions by Author  / Upsert Dimension Table / 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>Tue, 18 Jun 2013 16:01:06 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>&lt;P&gt;Hi everybody, apologise I came late to the discussion.Calvin, I'm not sure why the link is missing but the article is at &lt;A href="http://www.sqlservercentral.com/columnists/vRainardi/2732.asp"&gt;http://www.sqlservercentral.com/columnists/vRainardi/2732.asp&lt;/A&gt;. Steve, the link to the article is still missing today (7th Jan).&lt;/P&gt;&lt;P&gt;Uday, as Rasool said, to upsert a dimension in SSIS you can use SCD transformation such as &lt;A href="http://www.microsoft.com/library/media/1033/technet/images/prodtechnol/sql/2005/s4_big.png"&gt;http://www.microsoft.com/library/media/1033/technet/images/prodtechnol/sql/2005/s4_big.png&lt;/A&gt;, or as Alex and Robert said we can also use Lookup transformation.About your last post where an update is recognised as an insert, in the SSIS SCD Wizard did you choose the Change Type for that column as 'Changing Attribute' or as 'Historical Attribute'? And did you tick the 'Change all the matching records, including outdated records, when changes are detected in a changing attribute' check box?&lt;/P&gt;&lt;P&gt;Loner, the surrogate_key column which has 0 value in it is a temporary table, not the target dimension table. Yes you are right I forgot to include the natural key column. Thank you for your correction. The correct insert statement isINSERT INTO #dim (surrogate_key, natural_key, attribute1, attribute2, load_time)SELECT ISNULL(dim.surrogate_key,0), src.natural_key, src.attribute1, src.attribute2, @current_load_timeFROM stg.dbo.source srcLEFT JOIN dw.dbo.dimension dim ON src.natural_key = dim.natural_keyYes we can also do it using the approach that you described, Loner, i.e. the temp table doesn't have to have the surrogate key and we leave the key assignment to identity of the target dimension table. SCD3 is normally use if we only require certain level of history, for example, if we want to keep only the last 2 region for a particular branch. So if branch A is allocated to region 1 in 2001, then to region 2 in 2002 (perhaps because of reorganisation or restructurisation), then to region 3 in 2003, and we only want to keep the last 2 regions, then SCD3 could be a technique that we could use. So we don't normally add a column every time. If we want to keep the last 2 values for example, then we created 3 sets of columns since the beginning (1 set is for the current value). But you are right, if we want to keep the last 10 values then the structure would be a big table. &lt;/P&gt;&lt;P&gt;Cliff, I agree with Loner that we can use checksum instead of or, which is cleaner as you said. I am not sure that it is quicker though. Loner, you are right, we should use natural key when comparing rows in dimension table. The purpose of 'WHERE tmp.surrogate_key &amp;lt;&amp;gt; 0' is not for comparing, but to update the rows where the tmp table’s surrogate key is not 0, i.e. the rows already exist on the dimension table. For the rows where the surrogate key is 0 (not exist on the dimension table), we will insert them into the dimension table later on.&lt;/P&gt;&lt;P&gt;Ah, Uday, I saw that Rasool already explained 'Changing Attribute' or as 'Historical Attribute' and how to tag the current records, in his last posting.&lt;/P&gt;</description><pubDate>Sun, 07 Jan 2007 16:03:00 GMT</pubDate><dc:creator>VincentRainardi</dc:creator></item><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Times New Roman" size=3&gt;It all depends on the SCD type. If you chose TYPE I (overwrite) then in SSIS SCD wizard you chose &amp;lt;changing attributes&amp;gt; which basically means update existing record. However if you are dealing with TYPE II then you chose &amp;lt;Historical Attribute&amp;gt; which means add a new record. Here you must have a way to distinguish the current record from the old records. Usually we design the dimension table with fields such as &amp;lt;current_flag&amp;gt; and configure SCD component to set the flag to “y” or “n”. Now here the assumption is that your businees key is not your primary key. If it is then you can only use Type I SCD and have to configure the SCD component to &amp;lt;changing attributes&amp;gt; and not &amp;lt;Historical Attribute&amp;gt;. Hope this helps.&lt;/FONT&gt;&lt;/P&gt;</description><pubDate>Fri, 01 Dec 2006 08:46:00 GMT</pubDate><dc:creator>Rasool Omidakhsh</dc:creator></item><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>&lt;P&gt;cliffb,&lt;/P&gt;&lt;P&gt;I could use checksum, I was just in a hurry so I wrote it in using 'OR' especially there were only two values to check.  However I would not use surrograte key to compare when I update the dimension table, I would use the natural key.&lt;/P&gt;</description><pubDate>Wed, 29 Nov 2006 06:10:00 GMT</pubDate><dc:creator>Loner</dc:creator></item><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>&lt;P&gt;I tried using the Slowly Changing Dimension transormation. I basically need two paths - One to insert the new record (&lt;STRONG&gt;'New Output Path'&lt;/STRONG&gt; to OLE DB Destination) and one to update changed attributes in an existing record based on a specified business key (&lt;STRONG&gt;'Changed Attribute Updates Output'&lt;/STRONG&gt; to OLE DB Destination).&lt;/P&gt;&lt;P&gt;The insertion works fine but when i modify the same record in source data (retaining the same primary key) the data gets routed in the Changed Attribute path but fails with a primary key violation (it has attempted to insert into the destination table). Am i missing a setting or something that forces update rather than insert? &lt;/P&gt;</description><pubDate>Tue, 28 Nov 2006 22:39:00 GMT</pubDate><dc:creator>uday balaji</dc:creator></item><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>&lt;P&gt;Is there a reason you chose to use a list of "OR" vs CHECKSUM in your update statement? I realize CHECKSUM is not the same as CRC but it works just fine if comparing a single row.&lt;/P&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;P&gt;UPDATE dimSET dim.attribute1 = tmp.attribute1, dim.attribute1 = tmp.attribute2,dim.load_time = tmp.load_timeFROM dw.dbo.dimension dimINNER JOIN #dim tmpON tmp.surrogate_key = dim.surrogate_keyWHERE tmp.surrogate_key &amp;lt;&amp;gt; 0AND CHECKSUM(dim.attribute1,dim.attribute2) &amp;lt;&amp;gt; CHECKSUM(tmp.attribute1,tmp.attribute2)&lt;/P&gt;&lt;P&gt;This is cleaner to read for SCD Type 1 changes and, at least in our environment, seems to be a bit quicker. &lt;/P&gt;</description><pubDate>Tue, 28 Nov 2006 09:27:00 GMT</pubDate><dc:creator>cliffb</dc:creator></item><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>In SSIS you could use the &amp;lt;Slowly Changing Dimension&amp;gt; component which basically does the UPSERT. </description><pubDate>Tue, 28 Nov 2006 08:52:00 GMT</pubDate><dc:creator>Rasool Omidakhsh</dc:creator></item><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>&lt;P&gt;The surrograte key is an identity field, how can it have zero in it?&lt;/P&gt;&lt;P&gt;What I usually do is loading all the data in a staging table.  Then using left join to insert the new records into the dimension table. The dimension table does not need to have the surrogate key.&lt;/P&gt;&lt;P&gt;INSERT INTO Dim(Natural_key, attribute1, attribute2, load_time)SELECT s.natural_key,  s.attribute1, s.attribute2, GETDATE()FROM staging_table sLEFT JOIN dw.dbo.dimension dim ON s.natural_key = dim.natural_key WHERE dim.natural_key is NULL&lt;/P&gt;&lt;P&gt;BTW, your step 2 insert statement, did you forget to insert the natural_key?&lt;/P&gt;&lt;P&gt;If the records are already in the dimension table, then update the record with the most recent information.&lt;/P&gt;&lt;P&gt;Update d set d.attitute1=s.attitute1, d.attitute2 = s.attitute2, d.load_time=GETDATE() FROM dw.dbo.dimension d inner join staging_table s where (d.natural_key = s.natural_key) and (d.attitute1 &amp;lt;&amp;gt; s.attitute1 or d.attitute2 &amp;lt;&amp;gt; s.attitute2)&lt;/P&gt;&lt;P&gt;I don't know what kind of benefit to do SCD3.  I think the effective date and expiration date is good enough in dimension table.  In SCD3 you are changing the dimension table structure.  What if the attribute changes 10 times, you would have a big table.&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Tue, 28 Nov 2006 07:59:00 GMT</pubDate><dc:creator>Loner</dc:creator></item><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>OK, OK, apologies. That was the weirdest thing. The process we use to load the articles is generating an old one on disk for some reason. Did it again this morning and I didn't catch it last night. It's up there now.</description><pubDate>Tue, 28 Nov 2006 07:26:00 GMT</pubDate><dc:creator>Steve Jones - SSC Editor</dc:creator></item><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>Steve, I think one of your indexes is slightly sick.  We already know all about the sale.  Now, we need the Upsert article, please.(We all hope this is NOT a sign of things to come)Regards,Peter</description><pubDate>Tue, 28 Nov 2006 04:33:00 GMT</pubDate><dc:creator>Peter Kuzniewski</dc:creator></item><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>I'm not handling such large data. But will be mindful of that... thanks for the warning.</description><pubDate>Tue, 28 Nov 2006 04:31:00 GMT</pubDate><dc:creator>uday balaji</dc:creator></item><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>Oh cool! I should have thought of that before... thanks a lot :o)</description><pubDate>Tue, 28 Nov 2006 04:29:00 GMT</pubDate><dc:creator>uday balaji</dc:creator></item><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>&lt;P&gt;SSIS is memory hungry so is you use the LookUp trans will you will need to be mindful of how SSIS will chew all your memory.  I've used the process I outlined above on up to 10 million rows (which is not a lot) but SSIS manages this very well and very fast.&lt;/P&gt;&lt;P&gt;Simply build your package using Execute SQL taks between dataflows to faciliate my process.&lt;/P&gt;</description><pubDate>Tue, 28 Nov 2006 04:16:00 GMT</pubDate><dc:creator>Alan Hargreaves</dc:creator></item><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>&lt;P&gt;Hi, use Lookup transformation on SSIS data flow page with the referential table. It is possible to update in standard output flow or insert in configured error flow.&lt;/P&gt;&lt;P&gt;Robert&lt;/P&gt;</description><pubDate>Tue, 28 Nov 2006 03:59:00 GMT</pubDate><dc:creator>esbob cz</dc:creator></item><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>My question was not how do it in the DB directly. Im trying to figure out if there is a way in Sql Server Integration Services (DTS) to do the same using a transformation.</description><pubDate>Tue, 28 Nov 2006 03:46:00 GMT</pubDate><dc:creator>uday balaji</dc:creator></item><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>&lt;P&gt;Delete from the target table where the same data exists in the source table.  At this point if the source row = target row its ok.  If source row has updated values you dont care either becuase the next step is to insert all source rows to the target which is essentially overwritting rows that have been updated within the source.&lt;/P&gt;&lt;P&gt;The easiest way of all however would be to use some kind of auditing within you loads from source to target.  But we dont live in a perfect world.  &lt;/P&gt;&lt;P&gt;If I could VPN to a client site I would add a sample package but again, we dont live in a perfect world!!!!!&lt;img src='images/emotions/tongue.gif' height='20' width='20' border='0' title='Tongue' align='absmiddle'&gt;&lt;/P&gt;</description><pubDate>Tue, 28 Nov 2006 03:34:00 GMT</pubDate><dc:creator>Alan Hargreaves</dc:creator></item><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>Oh, upsert...thought this was the upskirt discussion.But seriously, what's wrong with the following?Update eset ColumnName = i.ColumnNamefrom ImportTable iJOIN TableName e on i.uniqueID = e.uniqueID Insert TableName (ColumnName)select i.ColumnNamefrom ImportTable iLEFT JOIN TableName e on i.uniqueID = e.uniqueID where e.uniqueID is nullDatabase 101, but is there some newfangled way to do this in the new Analytics?  You'd still need to build the dimension and fact tables using SQL (or something), right?</description><pubDate>Tue, 28 Nov 2006 03:07:00 GMT</pubDate><dc:creator>Calvin Lawson</dc:creator></item><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>&lt;P&gt;same player shoot again .... hit ball when lights are on &lt;img src='images/emotions/laugh.gif' height='20' width='20' border='0' title='Laugh' align='absmiddle'&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Tue, 28 Nov 2006 02:03:00 GMT</pubDate><dc:creator>ALZDBA</dc:creator></item><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>Ok.... i havent the slightest idea what that meant! </description><pubDate>Tue, 28 Nov 2006 01:50:00 GMT</pubDate><dc:creator>uday balaji</dc:creator></item><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>The article is printed in one of the fullstops via microfilm (lol)</description><pubDate>Tue, 28 Nov 2006 01:47:00 GMT</pubDate><dc:creator>John McC</dc:creator></item><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I urgently need to know how to carry out an UPSERT in SSIS 2005. If anyone knows the way to do it, please let me know as soon as possible.&lt;/P&gt;&lt;P&gt;TIA,&lt;/P&gt;&lt;P&gt;Uday.&lt;/P&gt;</description><pubDate>Tue, 28 Nov 2006 01:21:00 GMT</pubDate><dc:creator>uday balaji</dc:creator></item><item><title>RE: Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>Is it just me, or is the article missing?</description><pubDate>Tue, 28 Nov 2006 00:37:00 GMT</pubDate><dc:creator>Calvin Lawson</dc:creator></item><item><title>Upsert Dimension Table</title><link>http://www.sqlservercentral.com/Forums/Topic325468-292-1.aspx</link><description>Comments posted here are about the content posted at &lt;A HREF="temp"&gt;temp&lt;/A&gt;</description><pubDate>Sat, 25 Nov 2006 11:55:00 GMT</pubDate><dc:creator>VincentRainardi</dc:creator></item></channel></rss>