• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Read Once, Update Many

    November 30, 2011 Ted Holt

    Using SQL rather than native I/O to query and manipulate the database is more than replacing one syntactical regulation with another. It requires a different way of thinking. Nevertheless, corresponding features do differ, and today I show one way that the SQL update differs from a native update.

    Consider the following scenario from a project in which I was recently involved.

    I needed to read a database table (physical file) from top to bottom. After I retrieve each row (record), I called one or both of two high-level language programs, using parameters to pass data values from the table and to receive other data values in return. Depending on the values in the returned data, I had to update certain columns (fields). In other words, many different updates had to take place.

    To give you some idea of what I’m talking about, here is a much simplified RPG program that uses native I/O.

    FSomeFile  uf   e             disk    prefix(A_) usropn
    
    D CurrentTime     s               z   inz(*sys)
    D Status          s              1a
    
     /free
         *inlr = *on;
    
         open SomeFile;
    
         dow '1';
            read SomeRec;
            if %eof();
               leave;
            endif;
         //// do something here that sets Status
         //// and may or may not affect Balance
            A_RunTime = CurrentTime;
            if Status = '1';
               update SomeRec %fields(A_Balance: A_RunTime);
            else;
               update SomeRec %fields(A_RunTime);
            endif;
         enddo;
    
         close *all;
         return;
    

    In this simple program, there are two updates. The system will only carry out one of them for each record in the file, depending on the value of the status field. Notice that the runtime field is updated in either case.

    In my more complex example, I needed to carry out multiple updates. To return to my simplified example, I needed to do this:

            if Status = '1';
               update SomeRec %fields(A_Balance);
            endif;
            update SomeRec %fields(A_RunTime);
    

    But native I/O won’t allow more than one update for a single read. To make this code run, I would have to add an input operation before the second update.

    Updating a single row is one way that SQL differs from native I/O. When you update a table through a cursor, you can update as many times as you like, like this:

    D CurrentTime     s               z   inz(*sys)
    
    D WorkRec         ds                  qualified inz
    D   CustomerType                 1a
    D   TAmount                      7p 2
    D   Bal                          7p 2
    
    D Status          s              1a
    D Balance         s              7p 2
    
     /free
         *inlr = *on;
    
         exec sql
            declare c1 cursor for
               select c.custype, c.transamt, c.balance
                 from SomeFile as c
                  for update of balance, runtime;
    
         exec sql
            open c1;
    
         dow '1';
            exec sql
               fetch c1 into :WorkRec;
            if SqlState >= '02000';
               leave;
            endif;
    
         //// do whatever to change status and balance
    
            if Status = '1';
               exec sql
                  update SomeFile as c
                     set c.Balance = :Balance
                   where current of c1;
            endif;
            exec sql
               update SomeFile as c
                  set c.RunTime = :CurrentTime
                where current of c1;
         enddo;
    
         exec sql
            close c1;
         return;
    

    Notice the two back-to-back UPDATE commands. The fact that SQL allowed two updates for one fetch helped me in two ways. First, it eliminated the need to write a lot of messy conditional logic. Second, it permitted me to write a few, short UPDATE commands, rather than a lot of long ones.



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

    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:

    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

    Sponsored Links

    Micro Focus:  RUMBA for iSeries, the world's most used terminal emulation software
    The 400 School:  Fall Training Sale – Discounts up to 40%! RPG IV COBOL CL Admin Security
    Four Hundred Monitor Calendar:  Latest info on national conferences, local events, & Webinars

    IT Jungle Store Top Book Picks

    BACK IN STOCK: Easy Steps to Internet Programming for System i: List Price, $49.95

    The iSeries Express Web Implementer's Guide: List Price, $49.95
    The iSeries Pocket Database Guide: List Price, $59
    The iSeries Pocket SQL Guide: List Price, $59
    The iSeries Pocket WebFacing Primer: List Price, $39
    Migrating to WebSphere Express for iSeries: List Price, $49
    Getting Started with WebSphere Express for iSeries: List Price, $49
    The All-Everything Operating System: List Price, $35
    The Best Joomla! Tutorial Ever!: List Price, $19.95

    Starsoft Taps Veryant to Port IBM i COBOL to Open Systems Another IFS Interface

    Leave a ReplyCancel reply

Volume 11, Number 36 -- November 30, 2011
THIS ISSUE SPONSORED BY:

Bytware
ProData Computer Services
WorksRight Software

Table of Contents

  • Read Once, Update Many
  • Another IFS Interface
  • Why Are My Batch Jobs Running at Priority 20?

Content archive

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

Recent Posts

  • IBM i PTF Guide, Volume 28, Number 28: A Crazy Number of Security Vulnerability Patches
  • 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

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