Real Estate Data Warehouse – 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 Accelerating the Staging Process for your Data Warehouse
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.
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.
/***********************
Start of Script
************************/
/***********************
Configuration
************************/
DECLARE @sqlCommand varchar(1000)
DECLARE @DatabaseNameStaging varchar(75)
DECLARE @DatabaseNameSource varchar(75)
SET @DatabaseNameStaging = ‘Staging’
SET @DatabaseNameSource = ‘SourceDB’
— Add all tables to ignore to this list
DROP TABLE #TablestoIgnore
CREATE TABLE #TablestoIgnore
(
TableName varchar(255)
)
INSERT INTO #TablestoIgnore
Select ”
–UNION
–Select ”
— Table to Store List of all Table is Source Database
DROP TABLE #TableList
CREATE TABLE #TableList
(
TableName varchar(255)
)
/***********************
Create Staging Database
************************/
SET @sqlCommand = ‘IF NOT EXISTS(SELECT * FROM sys.databases WHERE NAME = ”’+@DatabaseNameStaging+”’)
BEGIN
CREATE DATABASE ‘+@DatabaseNameStaging+’
— Set Logging to Simple
USE master ;
ALTER DATABASE ‘+@DatabaseNameStaging+’ SET RECOVERY SIMPLE
END’
EXEC (@sqlCommand)
/***********************
Get List of All Tables
************************/
SET @sqlCommand = ‘INSERT INTO #TableList SELECT DISTINCT T.name AS Table_Name
FROM ‘+@DatabaseNameSource+’.sys.objects AS T
WHERE T.type_desc = ”USER_TABLE”
AND T.name NOT IN (SELECT TableName FROM #TablestoIgnore)
ORDER By 1′
EXEC (@sqlCommand)
–Create Drop and Create Statements
SELECT ‘IF OBJECT_ID(”’ + @DatabaseNameStaging + ‘.dbo.’+ TableName + ”’, ”U”) IS NOT NULL DROP TABLE ‘ + @DatabaseNameStaging + ‘.dbo.’+ TableName + ‘;’ AS DropStatement,
‘SELECT Top 1 * INTO ‘ + @DatabaseNameStaging + ‘.dbo.’+ TableName + ‘ From ‘ + @DatabaseNameSource + ‘.dbo.’+ TableName AS CreateStatement
INTO #DatabaseStatements
FROM #TableList
–Create Drop and Create Statements
— Run Drop Commands
DECLARE @MyCursor CURSOR;
DECLARE @MyField varchar(500);
BEGIN
SET @MyCursor = CURSOR FOR
SELECT DropStatement FROM #DatabaseStatements
OPEN @MyCursor
FETCH NEXT FROM @MyCursor
INTO @MyField
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC (@MyField)
FETCH NEXT FROM @MyCursor
INTO @MyField
END;
CLOSE @MyCursor ;
DEALLOCATE @MyCursor;
END;
— Run Create Commands
BEGIN
SET @MyCursor = CURSOR FOR
SELECT CreateStatement FROM #DatabaseStatements
OPEN @MyCursor
FETCH NEXT FROM @MyCursor
INTO @MyField
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC (@MyField)
FETCH NEXT FROM @MyCursor
INTO @MyField
END;
CLOSE @MyCursor ;
DEALLOCATE @MyCursor;
END;
/***********************
Create All Stored Procedures
to Load Staging
*** THIS SECTION MUST BE RUN AGAINST STAGING ENVIRONMENT ***
*** This step may result in Error for Identity Tables. Those ETL’s will need to be created
************************/
USE Staging
— Run Create Commands
BEGIN
SET @MyCursor = CURSOR FOR
SELECT TableName
FROM #TableList
OPEN @MyCursor
FETCH NEXT FROM @MyCursor
INTO @MyField
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC ( ‘IF OBJECT_ID(”spLoad’+@MyField+”’, ”P”) IS NOT NULL DROP PROC spLoad’+@MyField+”)
EXEC ( ‘TRUNCATE TABLE ‘+@DatabaseNameStaging+’.dbo.’+@MyField+’
CREATE PROCEDURE dbo.spLoad’+@MyField+’
AS
BEGIN
SET NOCOUNT ON;
— Insert statements for procedure here
INSERT INTO ‘+@DatabaseNameStaging+’.dbo.’+@MyField+’
SELECT * FROM ‘+@DatabaseNameSource+’.dbo.’+@MyField+’
END’)
EXEC ( ‘CREATE PROCEDURE dbo.spLoad’+@MyField+’
AS
BEGIN
SET NOCOUNT ON;
— Insert statements for procedure here
INSERT INTO ‘+@DatabaseNameStaging+’.dbo.’+@MyField+’
SELECT * FROM ‘+@DatabaseNameSource+’.dbo.’+@MyField+’
END’)
FETCH NEXT FROM @MyCursor
INTO @MyField
END;
CLOSE @MyCursor ;
DEALLOCATE @MyCursor;
END;
DROP TABLE #DatabaseStatements
/***********************
End of Script
************************/
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 Database, Data Lake, Data Factory, Data Bricks, Azure Synapse Data Warehouse and Power BI. Link to the class can be found here or directly here.
Part 1 – Navigating the Azure Portal
Part 2 – Resource Groups and Subscriptions
Part 3 – Creating Data Lake Storage
Part 4 – Setting up an Azure SQL Server
Part 5 – Loading Data Lake with Azure Data Factory
Part 6 – Configuring and Setting up Data Bricks
Part 7 – Staging data into Data Lake
Part 8 = Provisioning a Synapse SQL Data Warehouse
Part 9 – Loading Data into Azure Data Synapse Data Warehouse