• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Updating Through A Join With SQL, Take Three

    April 3, 2013 Ted Holt

    As I was looking for something in the DB2 for i SQL reference, my eyes happened to see MERGE in the list of statements, and I realized that I had been doing something the hard way. I realized that I was about to abandon the method I had been using to update values in one database table (physical file) from data in another one.

    Once in a while, someone requests that I use a list of values, which are usually stored in a spreadsheet, to mass update the database on the big computer. Fortunately, several years ago I learned how to use SQL to carry out such an update so that I don’t have to write a new RPG program every time. I have written about such updates before. See the Related Stories links at the bottom of this article.

    The methods I published do what I need them to do, but they are not intuitive and require a lot of head- and belly-scratching, especially when they include the EXISTS predicate. Using MERGE is better because MERGE was designed for cross-table updates.

    Let me illustrate by revisiting two examples that I used in my previous articles.

    Suppose I have a table of customers that includes, among other things, a sales region column.

    CUST ID   REGION
    =======  ======
     1         100
     2         100
     3         200
     4         100
     5         200
    

    The powers that be have taken the bold decision to renumber the sales regions. You have been asked to change the database in one broad stroke so that your employer won’t have to hire an army of temps to do the work manually.

    Here, in a table named FIXES, is the new region numbering scheme.

    OLDREGION  NEWREGION
    =========  =========
       100         20
       200         21
    

    And here is how you renumber the regions.

    merge into customer as c
      using (select oldregion, newregion from fixes) as f
         on c.region = f.oldregion
      when matched then
         update set c.region = f.newregion
    

    In the first line, you tell the computer that you are updating a table named CUSTOMER, which you call “c” for short.

    In the second and third line, you identify that old (existing) and new (replacement) values and tell how they join to the CUSTOMER table. The old region column of the FIXES table must match the region column in the CUSTOMER table. Using correlation name “f” to identify the data from the FIXES table simplifies the SQL command and clarifies your intention.

    In the fourth and fifth lines, you tell the computer how to update the database. When the computer finds a match, it is to update the REGION column in CUSTOMER with the value in the NEWREGION column of FIXES.

    After the merge, the customer data looks like this:

    CUST ID   REGION
    =======   ======
     1          20
     2          20
     3          21
     4          20
     5          21
    

    If there were customers with no match in FIXES, this MERGE statement would only update the customers that were in both tables. If you want to update customers that are not in the FIXES table, you will have to run a separate UPDATE command. Here are a couple of possibilities.

    1. Set the sales region to null for those customers who are not in the FIXES table.

    update customer as c
       set c.region = null
     where c.region not in
              (select oldregion from fixes)
    

    2. Set the sales region to 99 for the customers who are not in the FIXES table.

    update customer as c
       set c.region = 99
     where c.region not in
              (select oldregion from fixes)
    

    The second example is more like the requests I usually get.

    Assume that each vendor is assigned a type code of A, B, or C. Last week an accountant downloaded all the vendor data to a spreadsheet. He has modified the type code for some vendors and wants you to update the type code in the database.

    Here’s the vendor data before the update.

    ID   NAME                     TYPE
    ===  =======================  ====
    101  TOLSTOY IMPORTS          A
    102  DOSTOYEVSKY DONUTS       B
    103  CHECKOV TIRE CENTER      A
    104  GOGOL FOODS              B
    105  TURGENEV SPORTING GOODS  C
    

    Here are the new values.

    ID     TYPE
    ===    ====
    101    B
    102    B
    103    A
    104    A
    105    B
    

    As you can see, Tolstoy lost ground, but Gogol and Turgenev have moved up in the world.

    Here’s the command to update the database.

    merge into vendors as v
      using (select id, type from vendorupdt) as up
         on v.id = up.id
      when matched then
         update set v.type = up.type
      else ignore
    

    And here’s the updated database.

    ID   NAME                     TYPE
    ===  =======================  ====
    101  TOLSTOY IMPORTS          B
    102  DOSTOYEVSKY DONUTS       B
    103  CHECKOV TIRE CENTER      A
    104  GOGOL FOODS              A
    105  TURGENEV SPORTING GOODS  B
    

    It sure beats the EXISTS predicate, doesn’t it?

    For more information about MERGE, see Michael Sansoterra’s excellent article, Merge Into the Synchronization Fast Lane with DB2 for i 7.1.

    RELATED STORIES

    Merge Into the Synchronization Fast Lane with DB2 for i 7.1

    Updating Through a Join with SQL, Take Two

    Updating through a Join with SQL

    Update One File Based on Another File



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    BCD:  Recorded Webinar: How to mine IBM i report data & extract it to Excel
    Northeast User Groups Conference:  23nd Annual Conference, April 22 - 24, Framingham, MA
    COMMON:  Join us at the 2013 Conference & Expo, April 7 -10 in Austin, TX

    More IT Jungle Resources:

    System i PTF Guide: Weekly PTF Updates
    IBM i Events Calendar: National Conferences, Local Events, and Webinars
    Breaking News: News Hot Off The Press
    TPM @ The Reg: More News From ITJ EIC Timothy Prickett Morgan

    Attachmate Delivers Web Client for SFTP Tool Midrange Power7+ Servers: The IBM Sales Pitch

    Leave a Reply Cancel reply

Volume 13, Number 7 -- April 3, 2013
THIS ISSUE SPONSORED BY:

WorksRight Software
SEQUEL Software
Northeast User Groups Conference

Table of Contents

  • Bringing Home The Data
  • Updating Through A Join With SQL, Take Three
  • Admin Alert: A Checklist For Monitoring Your IBM i Environment

Content archive

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

Recent Posts

  • Public Preview For Watson Code Assistant for i Available Soon
  • COMMON Youth Movement Continues at POWERUp 2025
  • IBM Preserves Memory Investments Across Power10 And Power11
  • Eradani Uses AI For New EDI And API Service
  • Picking Apart IBM’s $150 Billion In US Manufacturing And R&D
  • FAX/400 And CICS For i Are Dead. What Will IBM Kill Next?
  • Fresche Overhauls X-Analysis With Web UI, AI Smarts
  • Is It Time To Add The Rust Programming Language To IBM i?
  • Is IBM Going To Raise Prices On Power10 Expert Care?
  • IBM i PTF Guide, Volume 27, Number 20

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