Click here to Skip to main content
15,867,308 members
Articles / Database Development / SQL Server

Composable DML and Merge Statement in SQL Server 2008

Rate me:
Please Sign up or sign in to vote.
4.50/5 (15 votes)
9 Aug 2009CPOL1 min read 32.8K   11   13
Simple example to show how Composable DML works in SQL Server 2008.

Introduction

In my article Merge Statement in SQL Server 2008, I tried to showcase the new features of SQL Server 2008 wherein using a single statement we can add/update records in our database table without explicitly checking for the existence of records to perform operations like Insert or Update.

In this article, let me explain Composable DML, another cool feature of SQL Server 2008.

Here are a few facts that you must know before starting to use Composable DML:

  • Combine multiple DML actions in one statement.
  • Have one statement operate on rows affected by another.
  • SQL Server 2008 introduces INSERT FROM DML.

Syntax

SQL
INSERT INTO [target table]
SELECT ... 
FROM ([INSERT | UPDATE | DELETE | MERGE statement with OUTPUT clause]) AS D 
WHERE … ;

Example

There is a Customer table in Head Office and also in Branch Office. Through End of Day processing, the Head Office table is updated with the Branch Office table data for changes like new customer registration and changes in existing customer info. The changes (New, Edit) are logged in an Audit table.

The example uses a Merge statement and Composable DML to perform update in the Head Office Customer table and also logs the updates in the Audit table.

SQL
-- Create Head Office Customer Table
CREATE TABLE tblCustomerMaster_HeadOffice(
 CustomerID INT PRIMARY KEY,
 CustomerName VARCHAR(100),
 PrimaryContact VARCHAR(100),
 ContactPhone VARCHAR(12));
 
-- Create Branch Office Customer Table
CREATE TABLE tblCustomerMaster_BranchOffice(
 CustomerID INT PRIMARY KEY,
 CustomerName VARCHAR(100),
 PrimaryContact VARCHAR(100),
 ContactPhone VARCHAR(12));
 
-- Create Audit Table
CREATE TABLE tblCustomersAudit (
 CustomerID INT,
 ChangeAction NVARCHAR(10),
 ChangeDate DATETIME DEFAULT CURRENT_TIMESTAMP,
 OldCustomerName VARCHAR(100),
 NewCustomerName VARCHAR(100),
 PRIMARY KEY(CustomerID , ChangeAction , ChangeDate ));
 
-- SQL Statement to Sync the Head Office Customers Table
-- using Brach Office Customers Table and 
-- Log into the CustomersAudit Table
INSERT INTO tblCustomersAudit(CustomerID, ChangeAction, 
            OldCustomerName, NewCustomerName) 
SELECT CustomerID, MergeAction, OldCustomerName, NewCustomerName
FROM (    

    MERGE INTO tblCustomerMaster_HeadOffice AS HOC   
        USING tblCustomerMaster_BranchOffice AS BOC        
        ON 
            HOC.CustomerID = BOC.CustomerID
    WHEN MATCHED AND HOC.CustomerName <> BOC.CustomerName
    -- Record exists, data different
    THEN                        
            UPDATE SET HOC.CustomerName = BOC.CustomerName, 
            HOC.PrimaryContact = BOC.PrimaryContact, 
            HOC.PrimaryContact = BOC.ContactPhone
    WHEN NOT MATCHED --Record in Branch Office and Not in Head Office
    THEN            
       INSERT (CustomerID, CustomerName, PrimaryContact, ContactPhone)
       VALUES (BOC.CustomerID, BOC.CustomerName, 
               BOC.PrimaryContact, BOC.ContactPhone)

    OUTPUT $action, 
        COALESCE(inserted.CustomerID, deleted.CustomerID),
        deleted.CustomerName, 
        inserted.CustomerName
 ) AS T(MergeAction, CustomerID, OldCustomerName, NewCustomerName)

Conclusion

I tried to showcase Composable DML in action along with Merge statements. This is pretty simple and easy to implement.

Hope you enjoyed this article. Happy programming!!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralRe: Good Article Pin
Robin_Roy17-Aug-09 15:46
Robin_Roy17-Aug-09 15:46 
GeneralMy Vote of 5 Pin
johnclark6410-Aug-09 17:05
johnclark6410-Aug-09 17:05 
GeneralRe: My Vote of 5 Pin
Robin_Roy17-Aug-09 15:46
Robin_Roy17-Aug-09 15:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.