• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Creating An RPG Stored Procedure That Returns A Result Set

    March 17, 2025 Mike Larsen

    In the previous article, I showed how to create an RPG stored procedure that uses parameters. In this article, I’m going to show how to create an RPG stored procedure that returns a result set, as we would likely encounter this scenario more frequently. The data used in this example comes from the Department table from IBM’s Sample database.

    I created an RPG program (Figure 1) that selects all rows from the Departments table. I didn’t add any selection criteria as I know there aren’t many rows in the table, but you may want to limit the number of rows selected when implementing a production program. The source code for the program is available to download.

    Figure 1. RPG program

    I start by declaring a cursor and indicating that I’m returning data to the client, or consumer on line 33. A cursor provides a way to access a result table. Lines 35 – 40 select columns from the Department table. Since some of the columns are nullable, I added some code to show N/A if the column is null. On line 43, I open the cursor and on line 46, I associate the cursor with the result set. That’s it for the RPG program.

    Next, I created an SQL script to catalog the RPG program as a stored procedure (Figure 2).

    Figure 2. Catalog the RPG program as a stored procedure

    On line 17, I’m telling the system that I’d like to create or replace a stored procedure that resides in library MLLIB. The stored procedure name is MLRSTRPROC.

    In the prior article, lines 18 and 19 were used to define the input and output parameters for the stored procedure. Since this procedure doesn’t have any parameters, I left these lines blank.

    Since I’m returning a result set in this procedure, I indicate that on line 21. There is only one result set being returned by this procedure, but you can return multiple result sets if you choose.

    Line 22 indicates the stored procedure will be utilizing an RPG program.

    Modifies SQL Data on line 23 indicates that the procedure can execute any SQL statement except statements that are not supported in procedures.

    Line 24 points to the location of the RPG program that will be invoked in the procedure

    In the comments section of the program (line 8), I’ve included the statement I used to create the stored procedure.

    I’m going to run this script in VS Code, but you could also run it in ACS. (Figure 3).

    Figure 3. Call the stored procedure from VS Code

    I’m using Db2 for i to execute the procedure. Since I’m not passing any parameters, there is nothing inside the parentheses. When I execute the script, the results are shown in Db2 for i (Figure 4).

    Figure 4. Stored procedure result

    With a small amount of code, I’ve built a useful tool that can be consumed from multiple platforms. In the next part, I’m going create a stored procedure that returns a result set just using SQL.

    Mike Larsen is a director of information technology at Auburn Pharmaceutical and has been working with IBM i systems for over 20 years. He specializes in RPG, CL, and SQL and recently has been working with PHP and Python. Current projects have given Mike the opportunity to work with generating and parsing XML and JSON from SQL and consuming SOAP and REST web services. Although his main area of expertise is on IBM i, Mike has a passion for learning other languages and how he can integrate other platforms with IBM i.

    RELATED STORIES

    Guru: Creating An RPG Stored Procedure With Parameters

    Guru: Creating A Web Service With Basic Authentication

    Guru: Parsing JSON That Has Spaces In The Key

    Guru: Partitioning Result Sets Using SQL

    Guru: Comparing IFS Directories Using SQL

    Guru: String Manipulation Using SQL

    Guru: Regular Expressions, Part 1

    Guru: Regular Expressions, Part 2

    Guru: Debugging SQL Stored Procedures With ACS

    Guru: Creating PDF Documents With Python

    Guru: Creating Excel Spreadsheets With Python

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: 400guru, DB2 for i, FHG, Four Hundred Guru, IBM i, RPG, SQL, VS Code

    Sponsored by
    New Generation Software

    Simplify IBM i Query, Reporting & Analytics

    RSVP to Attend a FREE Webinar

    Whether your company still uses IBM Query/400, DB2 Web Query, or a mix of tools, NGS-IQ can simplify your work.

    NGS-IQ enables you to automate and share data access, visualization, and analytics tasks.

    Attend this webinar and see how easily you can move forward with NGS-IQ.

    Webinar: April 24, 2025

    RSVP now.

    www.ngsi.com – 800-824-1220

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Shield Adds Task Automation To Message Monitoring Tool RISE For SAP Could Be A Boon For IBM’s PowerVS Cloud

    2 thoughts on “Guru: Creating An RPG Stored Procedure That Returns A Result Set”

    • ema tissani says:
      March 21, 2025 at 5:39 am

      btw… if you don’t need particular settings, you can straight “CALL theprogram” … without declaring a specific stub stored procedure…

      Reply
    • ema tissani says:
      March 21, 2025 at 5:46 am

      btw… you can also return an internal array ds, in case one need to generate i.e. a computed table on the fly for common sql client consumption

      Exec SQL Set Result Sets Array :resultSet For :rowCount Rows;

      Reply

    Leave a Reply Cancel reply

TFH Volume: 35 Issue: 10

This Issue Sponsored By

  • Maxava
  • DRV Tech
  • New Generation Software
  • WorksRight Software
  • Service Express

Table of Contents

  • Government Mainframes Versus DOGE: Showdown At The COBOL Corral
  • RISE For SAP Could Be A Boon For IBM’s PowerVS Cloud
  • Guru: Creating An RPG Stored Procedure That Returns A Result Set
  • Shield Adds Task Automation To Message Monitoring Tool
  • IBM i PTF Guide, Volume 27, Number 11

Content archive

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

Recent Posts

  • 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
  • SEU’s Fate, An IBM i V8, And The Odds Of A Power13
  • Tandberg Bankruptcy Leaves A Hole In IBM Power Storage
  • RPG Code Generation And The Agentic Future Of IBM i
  • A Bunch Of IBM i-Power Systems Things To Be Aware Of
  • IBM i PTF Guide, Volume 27, Numbers 21 And 22

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