• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: More About Merge

    November 5, 2018 Ted Holt

    I often read back through articles that have appeared in this august publication to look for errors and omissions. Such an expedition recently made me aware that I have not told you as much as I would like to about the SQL MERGE statement. Today I am pleased to provide more information.

    First I want to be sure that everybody understands is that you can add conditions to the WHEN MATCHED and WHEN NOT MATCHED expressions. That means that you do not have to treat all matched or unmatched rows in the same way. Look at this example:

    merge . . .
    when not matched and src.Action = 1 then
    . . .
    when matched and src.Action = 2 then
    . . .
    when matched and src.Action = 3 then
    . . .
    when matched then
    . . .
    when not matched then
    . . .
    

    Here are five tests: three for matched rows and two for unmatched rows. As with the CASE structure that we often use in SELECT statements, MERGE will execute the operation of the first condition that proves true. The last two tests are “catch-all” tests and execute only when the action column of the source dataset has an invalid value.

    The three periods following each test stand for an operation, and this brings me to the second thing I want to share today.

    You may remember from previous articles that MERGE is a combination of INSERT and UPDATE. Well, it’s more than that. There are four — not two — operations that MERGE can carry out. MERGE can also delete rows and raise error conditions. Here’s the full MERGE statement using all four operations.

    merge into releases as tgt
       using (select * from relupdates) as src
          on (tgt.PONumber = src.PONumber
         and  tgt.LineNumber = src.LineNumber
         and  tgt.ReleaseNumber = src.ReleaseNumber)
    when not matched and src.Action = 1 then
       insert (PONumber, LineNumber, ReleaseNumber,
               ReleaseDate, Quantity)
          values (src.PONumber, src.LineNumber, 
    	          src.ReleaseNumber, src.ReleaseDate,
    			  src.Quantity)
    when matched and src.Action = 2 then
       update set tgt.ReleaseDate = src.ReleaseDate,
                  tgt.Quantity    = src.Quantity
    when matched and src.Action = 3 then
       delete
    when matched then
       signal sqlstate '87501'
          set message_text = 'Error on matched'
    when not matched then
       signal sqlstate '87502'
          set message_text = 'Error on unmatched';
    

    This MERGE statement might be the sort of thing you would use in a purchasing application. The database has a file of blanket purchase order releases:

    create table releases
      (PONumber      dec(5),
       LineNumber    dec(3),
       ReleaseNumber dec(3),
       ReleaseDate   date,
       Quantity      dec(3),
      primary key (PONumber, LineNumber, ReleaseNumber));
    
    Order Line Release Date Quantity
    101 4 1 2018-11-05 12
    101 4 2 2018-11-12 10
    101 4 3 2018-11-19 8
    213 1 1 2018-11-12 6
    213 1 2 2018-11-19 8

    Another table contains a batch of changes to be applied to the releases.

    create table relupdates
      (Sequence      dec(3),
       PONumber      dec(5),
       LineNumber    dec(3),
       ReleaseNumber dec(3),
       Action        dec(1),
       ReleaseDate   date,
       Quantity      dec(3),
      primary key (Sequence));
    

    The ACTION column (field) tells what to do to the release.

    Action Description
    1 Add a release
    2 Change a release
    3 Delete a release

    A batch of transactions would look like this:

    Sequence Order Line Release Action Date Quantity
    1 101 4 3 2 2018-11-24 10
    2 101 4 4 1 2018-11-28 16
    3 213 1 2 3 2018-11-01  0
    4 213 1 7 2 2018-11-30 14

    The first three transactions are valid.

    Transaction 1 updates the date and quantity of an existing release.

    when matched and src.Action = 2 then
       update set tgt.ReleaseDate = src.ReleaseDate,
                  tgt.Quantity    = src.Quantity
    

    Transaction 2 adds a new release.

    when not matched and src.Action = 1 then
       insert (PONumber, LineNumber, ReleaseNumber,
               ReleaseDate, Quantity)
          values (src.PONumber, src.LineNumber, 
    	          src.ReleaseNumber, src.ReleaseDate,
    			  src.Quantity)
    

    Transaction 3 deletes an existing release.

    when matched and src.Action = 3 then
       delete
    

    Transaction 4 is invalid, as there is no release 7 for line 1 of purchase order 213. SIGNAL raises a condition with SQLSTATE 87502.

    when not matched then
       signal sqlstate '87502'
          set message_text = 'Error on unmatched';
    

    MERGE is powerful. The more I use it, the more I like it.

    RELATED STORIES

    A More Efficient Way To Merge With SQL

    The Powerful SQL Upsert

    Updating Through A Join With SQL, Take Three

    Merge Into the Synchronization Fast Lane with DB2 for i 7.1

    IBM Knowledge Center – MERGE

    IBM Knowledge Center – SIGNAL

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: 400, DB2 for i, FHG, Four Hundred Guru, IBM i, SQL

    Sponsored by
    DRV Tech

    Get More Out of Your IBM i

    With soaring costs, operational data is more critical than ever. IBM shops need faster, easier ways to distribute IBM applications-based data to users more efficiently, no matter where they are.

    The Problem:

    For Users, IBM Data Can Be Difficult to Get To

    IBM Applications generate reports as spooled files, originally designed to be printed. Often those reports are packed together with so much data it makes them difficult to read. Add to that hardcopy is a pain to distribute. User-friendly formats like Excel and PDF are better, offering sorting, searching, and easy portability but getting IBM reports into these formats can be tricky without the right tools.

    The Solution:

    IBM i Reports can easily be converted to easy to read and share formats like Excel and PDF and Delivered by Email

    Converting IBM i, iSeries, and AS400 reports into Excel and PDF is now a lot easier with SpoolFlex software by DRV Tech.  If you or your users are still doing this manually, think how much time is wasted dragging and reformatting to make a report readable. How much time would be saved if they were automatically formatted correctly and delivered to one or multiple recipients.

    SpoolFlex converts spooled files to Excel and PDF, automatically emailing them, and saving copies to network shared folders. SpoolFlex converts complex reports to Excel, removing unwanted headers, splitting large reports out for individual recipients, and delivering to users whether they are at the office or working from home.

    Watch our 2-minute video and see DRV’s powerful SpoolFlex software can solve your file conversion challenges.

    Watch Video

    DRV Tech

    www.drvtech.com

    866.378.3366

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    As I See It: Rethinking Retirement Cloud Provider Connectria In Major Partnership Push

    One thought on “Guru: More About Merge”

    • Glenn Gundermann says:
      December 21, 2023 at 9:43 am

      This is a very powerful command. Thanks for sharing this Ted. Is there a way to specify an insert of all the columns when not matched? Some tables can have hundreds of columns.

      Reply

    Leave a Reply Cancel reply

TFH Volume: 28 Issue: 74

This Issue Sponsored By

  • New Generation Software
  • UCG Technologies
  • SEA
  • Computer Keyes
  • Manta Technologies

Table of Contents

  • IBM i In The Land Of The Rising Sun
  • Cloud Provider Connectria In Major Partnership Push
  • Guru: More About Merge
  • As I See It: Rethinking Retirement
  • I Dare You To Keep Track Of Power Systems Memory Prices

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle