Click here to Skip to main content
Click here to Skip to main content

Composable DML and Merge Statement in SQL Server 2008

, 9 Aug 2009
Rate this:
Please Sign up or sign in to vote.
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

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.

-- 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)

About the Author

Robin_Roy
Other Brilliance Information Sdn Bhd
Malaysia Malaysia
Working as a Senior Consultant with Brilliance MSC, Malaysia.
Love to evaluate new technologies and implement the same.
Believe in sharing knowledge.

Comments and Discussions

 
GeneralWell approach PingroupMd. Marufuzzaman21-Aug-09 3:31 
GeneralRe: Well approach PinmemberRobin_Roy24-Aug-09 15:46 
GeneralNice article.. PinmemberTaslima Bagum17-Aug-09 5:46 
GeneralRe: Nice article.. PinmemberRobin_Roy17-Aug-09 15:45 
QuestionMoney Sign ($)? Pinmemberaspdotnetdev16-Aug-09 10:58 
AnswerRe: Money Sign ($)? PinmemberRobin_Roy16-Aug-09 17:38 
That is to get the merge action from the merge statement... Inserted/Deleted
GeneralRe: Money Sign ($)? Pinmemberaspdotnetdev17-Aug-09 20:04 
GeneralHelpful Pinmemberarvindjo11-Aug-09 23:38 
GeneralRe: Helpful PinmemberRobin_Roy17-Aug-09 15:45 
GeneralGood Article Pinmemberarvindcoolest10-Aug-09 17:16 
GeneralRe: Good Article PinmemberRobin_Roy17-Aug-09 15:46 
GeneralMy Vote of 5 Pinmemberjohnclark6410-Aug-09 17:05 
GeneralRe: My Vote of 5 PinmemberRobin_Roy17-Aug-09 15:46 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 10 Aug 2009
Article Copyright 2009 by Robin_Roy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid