• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • CHAIN vs SELECT INTO

    August 2, 2016 Chuck Luttor

    The average RPG developer can quickly become proficient in replacing RPG database operation codes with their SQL equivalents when undertaking new programming. In each installment of this series, I will visit an op code or set of op codes in order to prove my contention. First up today is CHAIN.

    I remember the CHAIN op code from System/3 Model 6 and Model 10 disk days. (Yes, I have been around for a long, long time.) It has been used extensively by every RPG programmer since then. It is the basic op code for random access. In the “old days” it was used extensively to access disk records by relative record number as well as by key. Probably no longer. Let us discuss the merits of keyed disk access via CHAIN vs the merits of the SQL equivalent, SELECT INTO.

    Part 1 – File Definition

    Explicit definition of files is required by RPG and not required by SQL. In fact, each SQL SELECT INTO can specify its own lock and isolation parameters, which we will review briefly in the clauses of the SELECT statement. If CHAIN(N) and UNLOCK are also used, then this is a wash as far as locking is concerned.

    Part 2 – Data Definition

    I always define my normalized records with an external data structure so that in debug I can see the whole record with one eval command. But this is not necessary in RPG, and it is necessary in SQL only if we are to conveniently access the row’s data. Otherwise, we must individually specify each column. If we have a 1000-column row, which is quite possible in a view or logical file with extensive joins, then specifying only the columns we want may provide a performance advantage. Beyond a dozen columns or so, I prefer to use a data structure, as my time is valuable. Such a view or logical file could save many CHAINs and SELECTs. Although the logical purists among us will argue that this is a requirement of SQL and not of RPG, I contend that there is no meaningful program that does not require some debugging of input/output, and therefore this data structure is also a requirement in RPG. Part 2 is a push (betting term for a draw) between opponents.

    Part 3 – Record or Row Access

    We have arrived at the heart of the matter. Compare, if you will, the CHAIN statement at line 124 with the SELECT INTO statement stating at line 127. Clearly the SELECT is more complex and for the unfamiliar will require some learning. Now also consider the effort that is required with CHAIN to accomplish the same things that this SELECT statement is capable of. We will consider each row as shown below.

    Keyword

    Parameter(s)

    Observations

    select

    *

    “SELECT *” reads all the columns of a table or view. We can
    just as easily specify individual fields separated by commas. For example: “
    select vendno,
    vendname“. This is very similar to RPG, where
    by default we read all fields and must specify the input record fields if we
    wish to read a subset.

    into

    :vendds

    Host program variables (referenceable to your RPG program)
    are distinguished from SQL variables/column names by putting a colon in front
    of them. This is similar to an op code with a data structure as its result.
    If you are accessing a master record in order to place some field(s) into a
    transaction record if and only if the master file row with the specified key exists,
    then you could do this “
    select vendname into :transvname
    where vendno
    = :transvno“.

    from

    VENDFILE

    Usually we would use the IBM i object
    naming rules. Then this would be taken as *LIBL/VENDFILE. We could specify
    LIBRARY/FILE also. We could also opt for SQL naming rules using a period as
    in LIBRARY.FILE.

    where

    vendno = :vendno

    This is the key list, or as in our example CHAIN, the key
    parameter. A bit of extra keying but surely no effort to learn. Again this
    where clause can be very
    powerful, and often replaces many IF statements that would follow the CHAIN.
    For example suppose there were many types of vendors and we needed to add transaction
    fields only for type ‘A’. We would specify “
    select vendname into :transvname where vendno
    = :transvno
    and vendtype = ‘A'”. We should also note that the SQL
    select will work even without an appropriate index by scanning the table
    rows.

    with

    NC

    This means no commit control and no lock. It is the default
    behavior, but it may not be what you want when concurrent access is an issue
    (i. e., you may not want to read uncommitted rows). Each SQL statement can have
    its own locking and isolation parameters. If you want exclusive locking you
    would code “
    with RR
    use and keep exclusive locks”. Then you would need to issue an UPDATE,
    DELETE or INSERT followed by COMMIT or a
    ROLLBACK.
    Isn’t it time to use commitment control anyway? Omitting this clause will
    default the SQL statement to the commitment control and isolation level set
    previously.

    fetch

    first row only

    Always
    include this clause for compatibility with CHAIN’s behavior for duplicates.
    Without the fetch first row only clause
    and
    if more than one row satisfies the
    select, SQL will return t
    he multiple rows
    returned error, SQLSTATE = ‘21000’
    or SQLCODE = -811, and the assignment of
    variables occurs unpredictably. An order by clause may be required to ensure
    that the correct
    row is the first one selected.

    Part 4 – Exception Handling

    For the straightforward found or not found condition, is there really anything to choose from? SQLCODE will be something other than zero for any conditions other than row found and all data transfer successful. SQL does, however, give the programmer access to a wide range of warnings, as shown above.

    Exception handling will be another day’s topic. There will be no substitute for familiarizing oneself with the SQL Reference Manual and the SQL Messages and Codes Manual in the same way as the RPG Reference Manual. They are all available online.

    Replacing CHAIN With SELECT INTO

    I used the IBM i, which was handy for me to generate and test my examples. It is at V7R1 with the latest Technology Refresh level, and my example code is fully free-format. I believe that every RPG programmer will understand the examples, even if they do not yet have access to V7R1 and/or full free-format.

    Learning to replace the typical CHAIN is remarkably easy (except for isolation, and that is not easy in any context). I venture to suggest that within the first 20 SELECTs, a considerable majority of programmers will have it in hand.

    Chuck Luttor is an RPGILE and SQL programming consultant practicing in the IBM i space for many years in the Toronto and southern Ontario, Canada region. He has extensive expertise in securities brokerage accounting and accounting in general. Major Canadian banks and brokerage houses have been amongst his clients. His main area of interest now is the migration from DDS-defined databases to SQL data definition language defined databases accessed using RPG on the IBM i.


    To SQL Or Not To SQL? That Is The Question That Faces Today’s RPG Programmer

    SQL has been a standard for relational database management and access across platforms since the 80s, and it has been offered for IBM i for almost 20 years. However it is still not in use by many IBM i shops. This is because DB2/400 was originally released with DDS and it wasn’t until the early 2000s that SQL started to perform better on IBM i.

    In the past 15 years, IBM has invested heavily in SQL on IBM i and is incorporating all new advances in the database into SQL. I recently heard Frank Soltis state that almost all database enhancements for the IBM i were made to SQL and that with only a few exceptions, DDS has not been enhanced since 2000. I will take his word for it.

    On top of that, IBM provided the Generate Data Definition Language (QSQGNDDL) API to generate the SQL data definition language statements from DDS years ago and there are many complimentary tools available to make this easy.

    So why are so many shops still using DDS? After all, how can we as IBM i developers hold our heads high and claim that we are doing the best jobs possible if we are missing out on the last 15 years of database advances that IBM has incorporated into SQL for i? We can be much more productive by using all the great additional capabilities that today’s SQL provides.

    As an RPG programmer, I believe that it is not only desirable but necessary to replace both DDS and RPG database access op codes with SQL Data Definition Language (DDL) and SQL input/output statements, at least within new programs. However, they are separate steps within the SQL project. Which is best done first?

    DDL redefinition of the existing database in and of itself is useful only for some hardware performance gains. SQL I/O in RPG programs can help programmers be more productive. The latter step should come first because programmers are much more valuable and costly than the hardware. And that step can be accomplished by the programmers themselves without anything more than management’s agreement and a measure of initiative. How many IT projects are that low cost and high return?

    To achieve the next step, DDL database redefinition, IBM has kindly arranged DB2 for i so that this can be done without any recompiling of the existing RPG programs. Database management tools are available to automate the tedious job of creating SQL to update and reformat database objects by providing “select the options” GUIs, which provide both documentation/cross reference and promotion tools. Database administrators still not required. However, that topic is for another day.

    Send your questions or comments for Chuck to Ted Holt via the IT Jungle Contact page.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Midrange Dynamics North America

    Git up to speed with MDChange!

    Git can be lightning-fast when dealing with just a few hundred items in a repository. But when dealing with tens of thousands of items, transaction wait times can take minutes.

    MDChange offers an elegant solution that enables you to work efficiently any size Git repository while making your Git experience seamless and highly responsive.

    Learn more.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    System i Developer:  RPG & DB2 Summit - October 4-6 2016 in Chicago. Register now!
    COMMON:  Open Source and Systems Management at the COMMON Forum. August 24-25 in Chicago.
    Four Hundred Monitor Calendar:  Latest info on national conferences, local events, & Webinars

    Systems Monitoring Made Easier, Better, GUI-er IBM i Fundamental Strategy Unchanged, Always Changing

    Leave a Reply Cancel reply

Volume 16, Number 17 -- August 2, 2016
THIS ISSUE SPONSORED BY:

WorksRight Software
T.L. Ashford
System i Developer

Table of Contents

  • CHAIN vs SELECT INTO
  • Give Me Fewer (Not More!) Parameters, Please!
  • Using Lateral Correlation To Define Expressions In DB2 For i

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