{"id":85,"date":"2018-06-27T21:09:56","date_gmt":"2018-06-27T21:09:56","guid":{"rendered":"http:\/\/jackofalltradesmasterofsome.com\/blog\/?p=85"},"modified":"2020-04-15T14:18:03","modified_gmt":"2020-04-15T14:18:03","slug":"real-estate-data-warehouse-accelerating-the-staging-process","status":"publish","type":"post","link":"http:\/\/jackofalltradesmasterofsome.com\/blog\/2018\/06\/27\/real-estate-data-warehouse-accelerating-the-staging-process\/","title":{"rendered":"Accelerating the Staging Process for your Data Warehouse"},"content":{"rendered":"<p><strong>Real Estate Data Warehouse \u2013 Accelerating the Staging Process<\/strong><\/p>\n<p>The script below can be used to build a staging environment for any sort of industry and not just real estate related databases. The specifics of a RE Data warehouse will be covered in future blog post. It will allow you to\u00a0Accelerating the Staging Process for your Data Warehouse<!--more--><\/p>\n<p>When starting the process to capture data analytics, whether you planning to eventually build a data warehouse or eventually feed a big data Hadoop cluster, it helps to stage your data away from your source systems. This provides many benefits; the primary being having a copy of data to work with and process that is no longer in the transactional operational system. Having large processes or queries running against your transactional operation system provides unnecessary risk, can introduce bottlenecks or slowdowns and even open security holes that may not be needed. When you have a staging environment, all you need is one service level account managed by IT security that has read access to the source systems. From there, building a scheduled refresh process to load this data as a blanket truncate and reload can be set up easily. Many tools for ETL can be used and robust auditing and scheduling should be set up but getting off the ground quickly to start prototyping and profiling your data will allow you to get moving a lot sooner and providing value to the business.<\/p>\n<p>For this reason, I wrote the SQL script below a while back to help me on new projects. Running this script against a linked server connection or a replicated database will quickly allow you to build a staging database with procedures to load all the data as truncate and reloads. This can then be wrapped in a master SQL procedure and scheduled, giving you a full Staging ETL process with out needing ETL tools. Remember, this is just an accelerator and will require some tweaking and optimization to get to a final state, but this should get you off the ground with your basic SQL based source systems.<\/p>\n<p>\/***********************<\/p>\n<p>Start of Script<\/p>\n<p>************************\/<\/p>\n<p>\/***********************<\/p>\n<p>Configuration<\/p>\n<p>************************\/<\/p>\n<p>DECLARE @sqlCommand varchar(1000)<\/p>\n<p>DECLARE @DatabaseNameStaging varchar(75)<\/p>\n<p>DECLARE @DatabaseNameSource varchar(75)<\/p>\n<p>SET @DatabaseNameStaging = &#8216;Staging&#8217;<\/p>\n<p>SET @DatabaseNameSource = &#8216;SourceDB&#8217;<\/p>\n<p>&#8212; Add all tables to ignore to this list<\/p>\n<p>DROP TABLE #TablestoIgnore<\/p>\n<p>CREATE TABLE #TablestoIgnore<\/p>\n<p>(<\/p>\n<p>TableName varchar(255)<\/p>\n<p>)<\/p>\n<p>INSERT INTO #TablestoIgnore<\/p>\n<p>Select &#8221;<\/p>\n<p>&#8211;UNION<\/p>\n<p>&#8211;Select &#8221;<\/p>\n<p>&#8212; Table to Store List of all Table is Source Database<\/p>\n<p>DROP TABLE #TableList<\/p>\n<p>CREATE TABLE #TableList<\/p>\n<p>(<\/p>\n<p>TableName varchar(255)<\/p>\n<p>)<\/p>\n<p>\/***********************<\/p>\n<p>Create Staging Database<\/p>\n<p>************************\/<\/p>\n<p>SET @sqlCommand = &#8216;IF NOT EXISTS(SELECT * FROM sys.databases WHERE NAME = &#8221;&#8217;+@DatabaseNameStaging+&#8221;&#8217;)<\/p>\n<p>BEGIN<\/p>\n<p>CREATE DATABASE &#8216;+@DatabaseNameStaging+&#8217;<\/p>\n<p>&#8212; Set Logging to Simple<\/p>\n<p>USE master ;<\/p>\n<p>ALTER DATABASE &#8216;+@DatabaseNameStaging+&#8217; SET RECOVERY SIMPLE<\/p>\n<p>END&#8217;<\/p>\n<p>EXEC (@sqlCommand)<\/p>\n<p>\/***********************<\/p>\n<p>Get List of All Tables<\/p>\n<p>************************\/<\/p>\n<p>SET @sqlCommand = &#8216;INSERT INTO #TableList SELECT DISTINCT T.name AS Table_Name<\/p>\n<p>FROM\u00a0\u00a0 &#8216;+@DatabaseNameSource+&#8217;.sys.objects AS T<\/p>\n<p>WHERE\u00a0 T.type_desc = &#8221;USER_TABLE&#8221;<\/p>\n<p>AND T.name NOT IN (SELECT TableName FROM #TablestoIgnore)<\/p>\n<p>ORDER By 1&#8242;<\/p>\n<p>EXEC (@sqlCommand)<\/p>\n<p>&#8211;Create Drop and Create Statements<\/p>\n<p>SELECT &#8216;IF OBJECT_ID(&#8221;&#8217; + @DatabaseNameStaging + &#8216;.dbo.&#8217;+ TableName +\u00a0 &#8221;&#8217;, &#8221;U&#8221;) IS NOT NULL DROP TABLE &#8216; + @DatabaseNameStaging + &#8216;.dbo.&#8217;+ TableName\u00a0 + &#8216;;&#8217; AS DropStatement,<\/p>\n<p>&#8216;SELECT Top 1 * INTO &#8216; + @DatabaseNameStaging + &#8216;.dbo.&#8217;+ TableName\u00a0 + &#8216; From &#8216; + @DatabaseNameSource + &#8216;.dbo.&#8217;+ TableName\u00a0 AS CreateStatement<\/p>\n<p>INTO #DatabaseStatements<\/p>\n<p>FROM #TableList<\/p>\n<p>&#8211;Create Drop and Create Statements<\/p>\n<p>&#8212; Run Drop Commands<\/p>\n<p>DECLARE @MyCursor CURSOR;<\/p>\n<p>DECLARE @MyField varchar(500);<\/p>\n<p>BEGIN<\/p>\n<p>SET @MyCursor = CURSOR FOR<\/p>\n<p>SELECT DropStatement FROM #DatabaseStatements<\/p>\n<p>OPEN @MyCursor<\/p>\n<p>FETCH NEXT FROM @MyCursor<\/p>\n<p>INTO @MyField<\/p>\n<p>WHILE @@FETCH_STATUS = 0<\/p>\n<p>BEGIN<\/p>\n<p>EXEC (@MyField)<\/p>\n<p>FETCH NEXT FROM @MyCursor<\/p>\n<p>INTO @MyField<\/p>\n<p>END;<\/p>\n<p>CLOSE @MyCursor ;<\/p>\n<p>DEALLOCATE @MyCursor;<\/p>\n<p>END;<\/p>\n<p>&#8212; Run Create Commands<\/p>\n<p>BEGIN<\/p>\n<p>SET @MyCursor = CURSOR FOR<\/p>\n<p>SELECT CreateStatement FROM #DatabaseStatements<\/p>\n<p>OPEN @MyCursor<\/p>\n<p>FETCH NEXT FROM @MyCursor<\/p>\n<p>INTO @MyField<\/p>\n<p>WHILE @@FETCH_STATUS = 0<\/p>\n<p>BEGIN<\/p>\n<p>EXEC (@MyField)<\/p>\n<p>FETCH NEXT FROM @MyCursor<\/p>\n<p>INTO @MyField<\/p>\n<p>END;<\/p>\n<p>CLOSE @MyCursor ;<\/p>\n<p>DEALLOCATE @MyCursor;<\/p>\n<p>END;<\/p>\n<p>\/***********************<\/p>\n<p>Create All Stored Procedures<\/p>\n<p>to Load Staging<\/p>\n<p>*** THIS SECTION MUST BE RUN AGAINST STAGING ENVIRONMENT ***<\/p>\n<p>*** This step may result in Error for Identity Tables. Those ETL&#8217;s will need to be created<\/p>\n<p>************************\/<\/p>\n<p>USE Staging<\/p>\n<p>&#8212; Run Create Commands<\/p>\n<p>BEGIN<\/p>\n<p>SET @MyCursor = CURSOR FOR<\/p>\n<p>SELECT TableName<\/p>\n<p>FROM #TableList<\/p>\n<p>OPEN @MyCursor<\/p>\n<p>FETCH NEXT FROM @MyCursor<\/p>\n<p>INTO @MyField<\/p>\n<p>WHILE @@FETCH_STATUS = 0<\/p>\n<p>BEGIN<\/p>\n<p>EXEC ( &#8216;IF OBJECT_ID(&#8221;spLoad&#8217;+@MyField+&#8221;&#8217;, &#8221;P&#8221;) IS NOT NULL DROP PROC spLoad&#8217;+@MyField+&#8221;)<\/p>\n<p>EXEC ( &#8216;TRUNCATE TABLE &#8216;+@DatabaseNameStaging+&#8217;.dbo.&#8217;+@MyField+&#8217;<\/p>\n<p>CREATE PROCEDURE dbo.spLoad&#8217;+@MyField+&#8217;<\/p>\n<p>AS<\/p>\n<p>BEGIN<\/p>\n<p>SET NOCOUNT ON;<\/p>\n<p>&#8212; Insert statements for procedure here<\/p>\n<p>INSERT INTO &#8216;+@DatabaseNameStaging+&#8217;.dbo.&#8217;+@MyField+&#8217;<\/p>\n<p>SELECT * FROM &#8216;+@DatabaseNameSource+&#8217;.dbo.&#8217;+@MyField+&#8217;<\/p>\n<p>END&#8217;)<\/p>\n<p>EXEC ( &#8216;CREATE PROCEDURE dbo.spLoad&#8217;+@MyField+&#8217;<\/p>\n<p>AS<\/p>\n<p>BEGIN<\/p>\n<p>SET NOCOUNT ON;<\/p>\n<p>&#8212; Insert statements for procedure here<\/p>\n<p>INSERT INTO &#8216;+@DatabaseNameStaging+&#8217;.dbo.&#8217;+@MyField+&#8217;<\/p>\n<p>SELECT * FROM &#8216;+@DatabaseNameSource+&#8217;.dbo.&#8217;+@MyField+&#8217;<\/p>\n<p>END&#8217;)<\/p>\n<p>FETCH NEXT FROM @MyCursor<\/p>\n<p>INTO @MyField<\/p>\n<p>END;<\/p>\n<p>CLOSE @MyCursor ;<\/p>\n<p>DEALLOCATE @MyCursor;<\/p>\n<p>END;<\/p>\n<p>DROP TABLE #DatabaseStatements<\/p>\n<p>\/***********************<\/p>\n<p>End of Script<\/p>\n<p>************************\/<\/p>\n<p>\u00a0<\/p>\n\n<p><br>\n<br>\n<!--StartFragment--><\/p>\n\n\n<p>Be sure to check out my full online class on the topic. A hands on walk through of a Modern Data Architecture using Microsoft Azure. For beginners and experienced business intelligence experts alike, learn the basic of navigating the Azure Portal to building an end to end solution of a modern data warehouse using popular technologies such as SQL&nbsp;Database, Data Lake, Data Factory, Data Bricks, Azure Synapse Data Warehouse and Power BI. Link to the class can be <a href=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/2020\/04\/08\/modern-data-architecture-using-microsoft-azure-online-class-and-free-ebook\/\">found here<\/a> or directly <a href=\"https:\/\/www.udemy.com\/course\/modern-data-architecture-using-microsoft-azure\/learn\/lecture\/18527998#overview\">here<\/a>.  <\/p>\n\n\n<p><!--StartFragment--><\/p>\n\n\n<p><a href=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/2020\/04\/08\/modern-data-architecture-part-1-navigating-the-azure-portal\/\">Part 1 &#8211; Navigating the Azure Portal<\/a><\/p>\n\n\n\n<p><a href=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/2020\/04\/13\/modern-data-architecture-part-2-resource-groups-and-subscriptions\/\">Part 2 &#8211; Resource Groups and Subscriptions<\/a><\/p>\n\n\n\n<p><a href=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/2020\/04\/13\/modern-data-architecture-part-3-creating-data-lake-storage\/\">Part 3 &#8211; Creating Data Lake Storage<\/a><\/p>\n\n\n\n<p><a href=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/2020\/04\/13\/modern-data-architecture-part-4-setting-up-a-sql-server\/\">Part 4 &#8211; Setting up an Azure SQL Server<\/a><\/p>\n\n\n\n<p><a href=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/2020\/04\/13\/modern-data-architecture-part-5-loading-data-lake-with-data-factory\/\">Part 5 &#8211; Loading Data Lake with Azure Data Factory<\/a><\/p>\n\n\n\n<p><a href=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/2020\/04\/13\/modern-data-architecture-part-6-configuring-and-setting-up-data-bricks\/\">Part 6 &#8211; Configuring and Setting up Data Bricks<\/a><\/p>\n\n\n\n<p><a href=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/2020\/04\/13\/modern-data-architecture-part-7-staging-data-into-data-lake\/\">Part 7 &#8211; Staging data into Data Lake<\/a><\/p>\n\n\n\n<p><a href=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/2020\/04\/14\/modern-data-architecture-part-8-provisioning-a-synapsis-sql-data-warehouse\/\">Part 8 = Provisioning a Synapse SQL Data Warehouse<\/a><\/p>\n\n\n\n<p><a href=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/2020\/04\/14\/modern-data-architecture-part-9-loading-data-synapse-data-warehouse\/\">Part 9 &#8211; Loading Data into Azure Data Synapse Data Warehouse<\/a><\/p>\n\n\n<p><!--EndFragment--><br>\n<br>\n<\/p>","protected":false},"excerpt":{"rendered":"<p>Real Estate Data Warehouse \u2013 Accelerating the Staging Process The script below can be used to build a staging environment for any sort of industry and not just real estate related databases. The specifics of a RE Data warehouse will be covered in future blog post. It will allow you to\u00a0Accelerating the Staging Process for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":87,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,5,11,3],"tags":[16,15,12,13,14],"class_list":["post-85","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-analytics","category-consulting","category-data-warehouse","category-real-estate","tag-accelerate","tag-big-data","tag-data-warehouse","tag-etl","tag-staging"],"_links":{"self":[{"href":"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/posts\/85","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/comments?post=85"}],"version-history":[{"count":8,"href":"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/posts\/85\/revisions"}],"predecessor-version":[{"id":778,"href":"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/posts\/85\/revisions\/778"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/media\/87"}],"wp:attachment":[{"href":"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/media?parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/categories?post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/tags?post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}