• 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:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

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

    Sponsored by
    JAMS Software

    One Scheduler. IBM i, Windows, Linux, and More.

    IBM i teams trust JAMS to schedule and orchestrate jobs across every platform in their environment. Centralized visibility, cross-platform dependency management, and alerts that reach the right person before the business feels it.

    Fewer than 5% of IBM i shops run IBM i only. The rest are managing cross-platform dependencies — often without a clear picture of how they connect. JAMS draws that map, enforces those dependencies automatically, and gives your team a single place to monitor, manage, and recover when something goes wrong.

    If you are running hundreds of CL scripts and custom RPG processes, bring them as-is. JAMS runs them exactly as they do today — except now they are visible, monitored, and part of an orchestrated workflow instead of scattered across folders only one person knows about.

    No consumption-based pricing. No surprise bills when your workload spikes. You pay based on how many machines JAMS talks to — that’s it.

    Learn More → https://jamsscheduler.com/lp/ibm-i

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) 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 ReplyCancel 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

  • Inside The Encryption Key Management Changes In IBM i 7.6
  • FalconStor Moved To The Blue Lagoon, And Is Poised For Growth Because Of It
  • Guru: Claude’s SQL Tip
  • Astera Makes Extracting Legacy Report Data an AI Specialty
  • IBM i PTF Guide, Volume 28, Number 27
  • Welcoming The New IBM i Chief Architect And Other New Top Brass
  • A Deep Dive Into That Power S1112 Entry Power11 Server
  • Guru: Beyond Three-Part Naming – Running SQL Across Remote IBM i Systems
  • How IBM Bolstered IBM i Resilience In The Summer Tech Refreshes
  • IBM i PTF Guide, Volume 28, Number 26

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