• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: An Update Conundrum Finally Solved!

    January 8, 2018 Ted Holt

    Russ writes, “Hey, Ted! I was trying to use a pure SQL solution to adjust some data today. My SQL statement worked fine in quality control, but failed in production. I’ve been wondering if the failure was caused by the database or by me! My problem was to renumber sequence numbers for a customer in a table.”

    Russ’s question arrived in my inbox on February 1, 2012. Yes, almost six years ago. At the time, I couldn’t help him. But with the latest technology refreshes from IBM, there is now a way to make the update work properly, and I’m about to show you how.

    First, let me explain Russ’s situation. A certain table (physical file) could contain more than one row (record) for each customer. Each row was uniquely identified by customer number and a three-digit packed decimal sequence number. A certain customer, to whom we’ll refer as customer 123, had rows with sequence numbers approaching 999, the limit. Russ needed to reorder the rows, starting at one, to eliminate the gaps and free up more sequence numbers at the end of the series.

    Here’s a table we can use to illustrate:

    create table mytable                         
      (customer char(3), 
       seq dec(3),
       otherdata char(12));
    
    insert into mytable values                             
    ('123', 10, 'S'),('123', 11, 'Q'),
    ('123', 12, 'L'),('123', 14, ' '),
    ('123', 21, 'R'),('123', 27, 'U'),
    ('123', 55, 'L'),('456', 10, 'Z'),
    ('456', 11, '!');
    

     

    Customer Sequence Other data
    123 10 S
    123 11 Q
    123 12 L
    123 14
    123 21 R
    123 27 U
    123 55 L
    456 10 Z
    456 11 !

     

    Russ decided to create a temporary table with new sequence numbers. The ROW_NUMBER OLAP function generates a new series of sequence numbers.

    declare global temporary table MyFix
      (cust, rownbr, orignbr)
    as
      (select customer,
              row_number() over(),
              seq
         from MyTable 
        where customer = '123'
        order by customer, seq)
    with data;
    

     

    Customer New sequence Current Sequence
    123 1 10
    123 2 11
    123 3 12
    123 4 14
    123 5 21
    123 6 27
    123 7 55

     

    Next he updated the production table from the temporary table.

    update MyTable as a
        set a.seq = (select rownbr
                       from session.MyFix
                      where a.seq = orignbr)
      where customer = '123';
    

     

    Customer Sequence Other data
    123 1 S
    123 2 Q
    123 3 L
    123 4
    123 5 R
    123 6 U
    123 7 L
    456 10 Z
    456 11 !

     

    Voilà! New sequence numbers for 123! The fact that the update worked properly during testing was pure luck. When this statement ran in production, the system threw a duplicate-key error when the update tried to change the sequence number of one row to the value of the sequence number of a row that had not yet been updated.

    Until now, DB2 updated the rows in no particular order. Russ needed a way to tell the UPDATE statement to update the selected rows in order by the sequence column, something that was impossible in 2012. Not anymore! The latest TR allows an UPDATE statement to have an ORDER BY clause.

    update MyTable as a
        set a.seq = (select rownbr
                       from session.MyFix
                      where a.seq = orignbr)
      where customer = '123'
      order by customer, seq;
    

    Let me tie this up with a couple more salient facts.

    (1) You can also use ORDER BY with the DELETE statement. This probably makes no sense unless you pair the ORDER BY with the LIMIT clause, which brings me to salient fact #2.

    (2) UPDATE and DELETE now support OFFSET and LIMIT. LIMIT specifies the maximum number of rows to update or delete. This query deletes the two rows that have the lowest sequence number.

    delete from mytable
     order by seq
     limit 2;
    

    OFFSET lets you tell DB2 how many rows to skip. This statement deletes up to two rows, beginning with the fourth row in the result set, for customer 123.

    delete from mytable
      where customer = '123'
      order by seq
      limit 2
      offset 3;
    

    If the sequence numbers for customer 123 are 10, 11, 12, 14, 21, 27 and 55, the system deletes 14 and 21.

    Russ ended up writing an update routine in RPG using record-level access, the same thing I have done in similar situations. Whether I will use RLA again for that task remains to be seen.

    RELATED STORIES

    SQL Paging With Limit And Offset In DB2 For i

    IBM i Technology Refresh – Information for IBM i 7.2

    IBM i Technology Refresh – Information for IBM i 7.3

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: 400guru, FHG, Guru, IBM i, RPG, SQL

    Sponsored by
    Midrange Dynamics North America

    With MDRapid, you can drastically reduce application downtime from hours to minutes. Deploying database changes quickly, even for multi-million and multi-billion record files, MDRapid is easy to integrate into day-to-day operations, allowing change and innovation to be continuous while reducing major business risks.

    Learn more.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    HelpSystems Adds To Its Non-IBM i Acquisition List Syncsort Bolsters IBM i Security Play With Cilasoft Buy

    3 thoughts on “Guru: An Update Conundrum Finally Solved!”

    • Jamie says:
      January 8, 2018 at 5:35 am

      Good to know!
      Do you know what TR(s) in which i versions add this support?

      Reply
    • Ken says:
      January 8, 2018 at 9:03 am

      Hello Ted,

      Happy New Year. Thanks for sharing. This will be quite helpful with some assignments coming my way.

      Ken

      Reply
    • RICHARD CARTER says:
      January 10, 2018 at 7:47 am

      Let’s all welcome DB2 to 1997!

      From the UPDATE statement Help topic for SQL Anywhere 5.5 published in 1997: “Normally, the order that rows are updated doesn’t matter. However, in conjunction with the NUMBER(*) function, an ordering can be useful to get increasing numbers added to the rows in some specified order. Also, if you wish to do something like add 1 to the primary key values of a table, it is necessary to do this in descending order by primary key, so that you do not get duplicate primary keys during the operation.”

      Reply

    Leave a Reply Cancel reply

TFH Volume: 28 Issue: 1

This Issue Sponsored By

  • ProData Computer Services
  • T.L. Ashford
  • WorksRight Software
  • Computer Keyes
  • Manta Technologies

Table of Contents

  • Looking Ahead To The IBM i New Year
  • Syncsort Bolsters IBM i Security Play With Cilasoft Buy
  • Guru: An Update Conundrum Finally Solved!
  • HelpSystems Adds To Its Non-IBM i Acquisition List
  • COMMON Education Gets Member-Driven Tweaks

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