• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Delimited List Processing in SQL

    March 24, 2004 Michael Sansoterra

    [The code for this article is available for download.]

    I recently worked on the database side of a Web project for which the Web developers needed to pass several item numbers to an iSeries for processing. Traditional methods would have dictated these items be inserted into a temporary or transaction file, then converted and processed as an XML file or passed individually to a stored procedure for processing.

    An alternative that is fast and painless (provided the related item information is minimal) is to make a single call to a stored procedure and to pass the item numbers in a delimited list as a single parameter (for instance, “ITEM1,ITEM2,ITEM3”). This approach is great in terms of performance but is awkward to process in SQL.

    You can remove the awkwardness by creating a user-defined table function to transform the delimited list into a set of rows. For the record, a table function (available in V5R2) is a program designed to supply data to SQL in a tabular format. Consider this table function:

    Create Function xxxxx/ListUDTF
                 (parmList     VarChar(24576),
                  parmDelim    VarChar(10),
                  parmMaxItems Integer)
    Returns Table(ItemNo Integer,
                  Item VarChar(128))
    External Name 'xxxxx/LISTUDTFR(PROCESSLIST)'
    Language RPGLE
    Disallow Parallel
    No SQL
    Parameter Style DB2SQL
    Deterministic
    

    The function accepts three parameters: a delimited list, a delimiter, and a maximum number of items to return. The RPG program LISTUDTFR transforms the list into rows such that the following statement:

    Select *
      From Table(ListUDTF('ITEM1,ITEM2,ITEM3',',',9999))
           As List
    

    Returns the following table:

    ItemNo Item
    1 ITEM1
    2 ITEM2
    3 ITEM3

    The function can then be used in a stored procedure to create multiple order lines based on a delimited list of items and order quantities, such that the Web developers only call the procedure once:

    Create Procedure spAddWebOrder
    (parmOrderID Integer,
     parmItems   VarChar(1600),
     parmQtys    VarChar(1600))
    Language SQL
    Modifies SQL Data
    Set Option Commit=*None
    Begin
        Insert Into OrderLines
        Select parmOrderID, Items.Item, 
               Cast(Qtys.Item As Integer) As Qty,
               Part.ListPrice
          From Table(ListUDTF(parmItems,',',9999)) 
               As Items
          Join Table(ListUDTF(parmQtys,',',9999))
               As Qtys On Qtys.ItemNo=Items.ItemNo
          Join PartMaster
               As Part On Part.PartNo=Items.Item
    End
    

    As you can see, the possibilities for delimited list processing are enormous. To tinker with this table function yourself, download and compile RPG program ListUDTFR. Follow the instructions in the header for registering the function (using the CREATE FUNCTION statement).

    In terms of programming, an external table function (written in a high-level language) is similar to an external scalar function, except that it can return multiple column values and multiple rows.

    When invoked in an SQL statement, SQL will call the function’s program with a pre-defined call type (OPEN, FETCH, CLOSE.) When a fetch is requested, the program is expected to fill in the output parameters to build one row of data and exit. This process is repeated until the program sets the SQLState parameter to 02000, which indicates that it has no more data to return.

    Further, the table function is given opportunity to do initialization and cleanup work (during the OPEN and CLOSE calls), which occur once at the beginning and end of each function invocation. During the open, any initialization work should be performed. During the close, all cleanup work should be performed and, in the case of an RPG program, *INLR should be set on to end the program completely.

    For help understanding how to build SQL function parameter lists, see the article “Scribble on SQL’s Scratchpad.” For more information on creating table functions, see the SQL Reference (in PDF format) and SQL Programming Concepts (also in PDF format) manuals.

    Michael Sansoterra is a programmer/analyst for SilverLake Resources, an IT services firm based in Grand Rapids, Michigan. E-mail: msansoterra@silver-lake.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Maxava

    Migrate IBM i with Confidence

    Tired of costly and risky migrations? Maxava Migrate Live minimizes disruption with seamless transitions. Upgrading to Power10 or cloud hosted system, Maxava has you covered!

    Learn More

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Chrono-Logic Offers Cross Reference Tool for LANSA Early Adopters Prep for New OS/400, iSeries, and Logical Partitions

    One thought on “Delimited List Processing in SQL”

    • Carlos Irigoyen says:
      August 14, 2017 at 4:13 pm

      Hi Michael: For your article ‘Delimited List Processing in SQL’, is there anyway that I could get the code. Thank you so much. Carlos

      Reply

    Leave a Reply Cancel reply

Volume 4, Number 9 -- March 24, 2004
THIS ISSUE
SPONSORED BY:

T.L. Ashford
Guild Companies
COMMON
WorksRight Sofware

Table of Contents

  • V5R3 CL Programming Enhancements, Part 2
  • Better Data Transfers
  • Delimited List Processing in SQL
  • OS/400 Alert: Microsoft Closes the Doors on JVM

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