The MERGE clause is the most powerful SQL clause known to man. With MERGE, you can take data from one table and load it into another table. Unlike INSERT INTO SELECT, MERGE gives you way more power in how you go about doing that.
With great power comes great complexity and a full explanation of SQL MERGE is FAR outside the scope of this tutorial.
The most common use case I have for MERGE is loading Type II slowly changing dimensions in data warehouses. I talk about this extensively in the section titled Loading A Type II Slowly Changing Dimension With SQL Merge.
The process of using MERGE works like this.
A Basic MERGE Example
USE demo
DROP TABLE IF EXISTS Person
DROP TABLE IF EXISTS PersonStageTable
CREATE TABLE Person(
PersonID BIGINT NOT NULL,
FirstName NVARCHAR(50) NULL,
LastName NVARCHAR(50) NULL,
SourceSystemKey NVARCHAR(50) NULL,
)
CREATE TABLE PersonStageTable(
PersonID BIGINT NOT NULL,
FirstName NVARCHAR(50) NULL,
LastName NVARCHAR(50) NULL,
SourceSystemKey NVARCHAR(50) NULL,
)
INSERT INTO Person(PersonID, FirstName, LastName, SourceSystemKey)
SELECT 1, 'Bob', 'Wakefield',1
INSERT INTO PersonStageTable(PersonID, FirstName, LastName, SourceSystemKey)
SELECT 1,'Bob','Johnson',1
UNION
SELECT 2,'Sally','Ride',2
SELECT * FROM Person
SELECT * FROM PersonStageTable
--*****Merge example beings here.*****
MERGE Person AS target
USING (
SELECT
PersonID,
FirstName,
LastName,
SourceSystemKey
FROM PersonStageTable
) AS source
ON (target.SourceSystemKey = source.SourceSystemKey)
WHEN NOT MATCHED THEN
INSERT (
PersonID,
FirstName,
LastName,
SourceSystemKey
)
VALUES (
PersonID,
FirstName,
LastName,
SourceSystemKey
)
WHEN MATCHED THEN
UPDATE
SET
target.PersonID = source.PersonID,
target.FirstName = source.FirstName,
target.LastName = source.LastName,
target.SourceSystemKey = source.SourceSystemKey
;
SELECT * FROM Person
DROP TABLE Person
DROP TABLE PersonStageTable
Copyright © 2020, Mass Street Analytics, LLC. All Rights Reserved.