{"id":129,"date":"2008-07-14T18:23:45","date_gmt":"2008-07-14T18:23:45","guid":{"rendered":"https:\/\/www.wapshere.com\/missmiis\/?p=129"},"modified":"2023-01-16T05:51:37","modified_gmt":"2023-01-16T05:51:37","slug":"sql-2005-ma-export-and-delta-import","status":"publish","type":"post","link":"https:\/\/www.wapshere.com\/missmiis\/sql-2005-ma-export-and-delta-import","title":{"rendered":"SQL 2005 MA: Export and Delta Import"},"content":{"rendered":"<p>I use a lot of SQL MAs. While ILM\/MIIS is great at syncing data between directories, it really isn&#8217;t that great at calculating and comparing. As much as possible I like to do the data manipulation in SQL, and then keep ILM to what it&#8217;s best at &#8211; joins and syncs.<\/p>\n<p>After exporting data to a SQL table you must follow with an Import step, allowing ILM to confirm that the data was correctly exported.\u00a0 For speed and efficiency you will want that confirming Import to be a Delta Import. This post shows you how to do that with SQL 2005.<\/p>\n<p><!--more--><\/p>\n<h3>Approach<\/h3>\n<p>You can only enable delta imports from a SQL MA if you have a Delta table. The simplest thing is to compare the table with an earlier version and create a Delta table from the differences.<\/p>\n<p>The trick is then to generate the Delta table in between running the export and delta import steps.<\/p>\n<h3>Create Previous and Delta Tables<\/h3>\n<p>Let&#8217;s start with a table called<em> MyTable<\/em>.<\/p>\n<p>You need to create an exact copy of this table to put the previous data set into &#8211; lets&#8217; call it <em>MyTable_Previous<\/em>.<\/p>\n<p>And finally you will need a delta table-<em> MyTable_Delta<\/em>. This has all the same columns as <em>MyTable<\/em>, plus an extra column called <strong>ChangeType<\/strong>, which will contain the strings &#8216;Add&#8217;, &#8216;Delete&#8217; or &#8216;Modify&#8217;.<\/p>\n<blockquote><p>SQL newbie tip: If you Edit the table in SQL Server Management Studio it will open a query window with a CREATE TABLE query, which you can then modify to create the extra tables. For the Delta table, as well as changing the table name, add this column definition:<\/p>\n<p><span style=\"font-size: x-small;\"><span style=\"font-size: x-small;\">[changeType] [nvarchar]<\/span><\/span><span style=\"font-size: x-small; color: #808080;\">(<\/span><span style=\"font-size: x-small;\">20<\/span><span style=\"font-size: x-small; color: #808080;\">)<\/span> <span style=\"font-size: x-small; color: #808080;\">NULL<\/span><\/p><\/blockquote>\n<h3>Create SSIS Packages<\/h3>\n<p>Next you need to create two SSIS packages:<\/p>\n<ol>\n<li>Generate_Delta &#8211; compares the current and previous versions of the table, and uses the differences to generate the Delta table.<\/li>\n<li>Clear_Delta &#8211; I like to separate out the step to clear all lines from the Delta table so I can make sure the import was successful before I run it.<\/li>\n<\/ol>\n<h4>Generate_Delta<\/h4>\n<p>This package contains two Execute SQL tasks &#8211; follow the links to see the SQL queries<\/p>\n<table border=\"0\">\n<tbody>\n<tr>\n<td><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.wapshere.com\/images\/ssis_generate_delta.GIF\" alt=\"\" width=\"157\" height=\"161\" \/><\/td>\n<td>\n<ol>\n<li><a href=\"https:\/\/www.wapshere.com\/missmiis\/?page_id=130\" target=\"_blank\" rel=\"noopener noreferrer\">Generate Delta<\/a> &#8211; compare MyTable to MyTable_Previous and put the results in MyTable_Delta.<\/li>\n<li><a href=\"https:\/\/www.wapshere.com\/missmiis\/?page_id=131\" target=\"_blank\" rel=\"noopener noreferrer\">Archive Table<\/a> &#8211; copy MyTable to MyTable_Previous, for the next time.<\/li>\n<\/ol>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>Clear_Delta<\/h4>\n<p>A very simple package &#8211; just the one Execute SQL task with a single query line:<\/p>\n<blockquote><p>truncate table MyTable_Delta<\/p><\/blockquote>\n<h3>Configure Your MA<\/h3>\n<p>Once you have the Delta table you can configure your MA for delta imports. I will just refer you to <a href=\"https:\/\/www.wapshere.com\/missmiis\/?p=97\">this post <\/a>for more info on that.<\/p>\n<h3>Configure Run Profiles<\/h3>\n<p>Fairly obvious so just mentioning for completeness &#8211; you&#8217;ll need to create one or more run profiles with a &#8220;Delta Import&#8221; step.<\/p>\n<h3>Running the SSIS packages with the ILM jobs<\/h3>\n<p>I now use a vbscript to put all the steps together. Follow the links to see the subroutines.<\/p>\n<pre>  If <a href=\"https:\/\/www.wapshere.com\/missmiis\/?page_id=125\">ExportsPending<\/a>(\"My_SQL2005_MA\") Then\n\u00a0   <a href=\"https:\/\/www.wapshere.com\/missmiis\/?page_id=122\">RunProfile<\/a> \"My_SQL2005_MA\", \"Export\"\n    <a href=\"https:\/\/www.wapshere.com\/missmiis\/?page_id=123\">RunSSIS<\/a> \"Generate_Delta\"\n\u00a0   If RunProfile(\"My_SQL2005_MA\", \"Delta Import and Delta Sync\") = 0 Then\n\u00a0     RunSSIS \"Clear_Delta\"\n\u00a0   End If\n\u00a0 End If<\/pre>\n<p>And that&#8217;s it!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I use a lot of SQL MAs. While ILM\/MIIS is great at syncing data between directories, it really isn&#8217;t that great at calculating and comparing. As much as possible I like to do the data manipulation in SQL, and then keep ILM to what it&#8217;s best at &#8211; joins and syncs. After exporting data to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[34,28,5],"tags":[],"class_list":["post-129","post","type-post","status-publish","format-standard","hentry","category-ilm2007","category-miis2003","category-sql"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pkp1o-25","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":4,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"predecessor-version":[{"id":3331,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/129\/revisions\/3331"}],"wp:attachment":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/tags?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}