[ WITH <common_table_expression> [,...n] ]
MERGE
[ TOP ( expression ) [ PERCENT ] ]
[ INTO ] target_table [ [ AS ] table_alias ]
[ WITH ( <merge_hint> ) ]
USING <table_source>
ON <search_condition>
[ WHEN MATCHED [ AND <search_condition> ]
THEN <merge_matched> ]
[ WHEN [TARGET] NOT MATCHED [ AND <search_condition> ]
THEN <merge_not_matched> ]
[ WHEN SOURCE NOT MATCHED [ AND <search_condition> ]
THEN <merge_matched> ]
<output_clause>
[ OPTION ( <query_hint> [ ,...n ] ) ]
;
-- Static table as target for merging data
CREATE TABLE CentralOfficeAccounts (
account_nbr INT PRIMARY KEY,
company_name VARCHAR(35),
primary_contact VARCHAR(35),
contact_phone VARCHAR(12));
-- Dynamic table with daily updates to be merged
CREATE TABLE BranchOfficeAccounts (
account_nbr INT PRIMARY KEY,
company_name VARCHAR(35),
primary_contact VARCHAR(35),
contact_phone VARCHAR(12));
-- Sample centrall office static data
INSERT INTO CentralOfficeAccounts
VALUES (1, 'Bayside Motors', 'Jim Smith', '902-203-1234'),
(2, 'Dallas Industries', 'Joe Doe', '301-663-9134'),
(3, 'Sky Computer Systems', 'Jane Brown', '201-943-6053');
-- Daily updated branch office data
INSERT INTO BranchOfficeAccounts
VALUES (2, 'Dallas Industries, Inc.', 'Rick Gross', '301-663-9134'), -- changed
(3, 'Sky Computer Systems', 'Jane Brown', '201-943-6053'), -- same
(4, 'Auto Insurance Co.', 'Chris Jefferson', '313-601-6201'); -- new
-- Update existing and add missing
MERGE INTO CentralOfficeAccounts AS C -- Target
USING BranchOfficeAccounts AS B -- Source
ON C.account_nbr = B.account_nbr
WHEN MATCHED THEN -- On match update
UPDATE SET C.company_name = B.company_name,
C.primary_contact = B.primary_contact,
C.contact_phone = B.contact_phone
WHEN NOT MATCHED THEN -- Add missing
INSERT (account_nbr, company_name, primary_contact, contact_phone)
VALUES (B.account_nbr, B.company_name, B.primary_contact, B.contact_phone);
SELECT account_nbr, company_name, primary_contact, contact_phone
FROM CentralOfficeAccounts;
/*
-- Results:
account_nbr company_name primary_contact contact_phone
----------- ------------------------ ---------------- -------------
1 Bayside Motors Jim Smith 902-203-1234
2 Dallas Industries, Inc. Rick Gross 301-663-9134
3 Sky Computer Systems Jane Brown 201-943-6053
4 Auto Insurance Co. Chris Jefferson 313-601-6201
*/
-- Update existing that changed and add missing
-- Use of predicates
MERGE INTO CentralOfficeAccounts AS C -- Target
USING BranchOfficeAccounts AS B -- Source
ON C.account_nbr = B.account_nbr
WHEN MATCHED -- On match update
AND (C.company_name <> B.company_name -- Additional search conditions
OR C.primary_contact <> B.primary_contact
OR C.contact_phone <> B.contact_phone) THEN
UPDATE SET C.company_name = B.company_name,
C.primary_contact = B.primary_contact,
C.contact_phone = B.contact_phone
WHEN NOT MATCHED THEN -- Add missing
INSERT (account_nbr, company_name, primary_contact, contact_phone)
VALUES (B.account_nbr, B.company_name, B.primary_contact, B.contact_phone);
--Update existing that changed and add missing, delete missing in source
MERGE INTO CentralOfficeAccounts AS C -- Target
USING BranchOfficeAccounts AS B -- Source
ON C.account_nbr = B.account_nbr
WHEN MATCHED -- On match update
AND (C.company_name <> B.company_name -- Additional search conditions
OR C.primary_contact <> B.primary_contact
OR C.contact_phone <> B.contact_phone) THEN
UPDATE SET C.company_name = B.company_name,
C.primary_contact = B.primary_contact,
C.contact_phone = B.contact_phone
WHEN NOT MATCHED THEN -- Add missing
INSERT (account_nbr, company_name, primary_contact, contact_phone)
VALUES (B.account_nbr, B.company_name, B.primary_contact, B.contact_phone)
WHEN SOURCE NOT MATCHED THEN -- Delete missing from source
DELETE;
SELECT account_nbr, company_name, primary_contact, contact_phone
FROM CentralOfficeAccounts;
/*
-- Results:
account_nbr company_name primary_contact contact_phone
----------- ------------------------ ---------------- -------------
2 Dallas Industries, Inc. Rick Gross 301-663-9134
3 Sky Computer Systems Jane Brown 201-943-6053
4 Auto Insurance Co. Chris Jefferson 313-601-6201
*/
Labels: sql server 2008, t-sql programming