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

    May 12, 2025 Mike Larsen

    Continuing to build out the stored procedure series, I’m going to show how to create an SQL stored procedure that returns a result set. This is very similar to the previous article where I showed how to do this using RPG. In this example, we’re just using pure SQL to achieve the same results.

    I created an SQL script (Figure 1) that selects all rows from the Departments table. The SQL statement is the same as the one I used in the RPG program from the prior article, but I’ll describe it again here. 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 script is available to download.

    Figure 1. SQL script

    On line 12, I direct the script to be either created if it’s not yet created, or to replace it if it already exists.

    On lines 13 and 14, I indicate that this stored procedure is using SQL and it returns one result set.

    This story contains code, which you can download here.

    Line 18 declares a cursor and indicates that I’m returning data to the client, or consumer. A cursor provides a way to access a result table. Lines 20 – 27 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 29, I open the cursor so it can be used to process rows from its result table.

    As I did in the prior article, I run this script in VS Code Db2 for i. (Figure 2).

    Figure 2. Call the stored procedure from VS Code

    The call statement is the same as before. I’m not passing any parameters to the stored procedure, so there is nothing inside the parentheses. When I execute the script, the results are shown in Db2 for i (Figure 3).

    Figure 3. Stored procedure result

    The results are the same as the result set from the RPG stored procedure, as expected. Why did I show both? My intent was to show multiple ways of achieving the same goal as you may be more comfortable one way or the other.

    This concludes the stored procedure series. I have added these tools to my arsenal. Hopefully they can help you, too.

    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 That Returns A Result Set

    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, IBM i, RPG, SQL, VS Code

    Sponsored by
    ARCAD Software

    Modern IBM i development is no longer about choosing between reliability and agility. With ARCAD, IBM i teams can adopt true Git-based DevOps while preserving the control, automation, and stability their business-critical applications require.

    In this short customer video, hear directly from organizations including HSBC, Heartland Co-op, and BWI as they share how ARCAD helped them transform their development and delivery processes.

    Their results speak for themselves: shorter delivery times, reduced downtime, improved developer efficiency, better traceability, streamlined release processes, and easier rollback when needed.

    From Git integration with platforms such as GitHub, GitLab, Bitbucket, and Azure DevOps, to parallel development, automated deployment, and modernized IBM i workflows, ARCAD enables development teams to move faster without compromising quality or governance.

    Don’t just take our word for it. Hear what ARCAD customers have to say.

    Watch the 4-minute video now.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    As I See It: At Any Cost Maxava Consulting Services Does More Than HA/DR Project Management – A Lot More

    Leave a Reply Cancel reply

TFH Volume: 35 Issue: 18

This Issue Sponsored By

  • Rocket Software
  • Maxava
  • ARCAD Software
  • Raz-Lee Security
  • WorksRight Software

Table of Contents

  • POWERUp 2025 –Your Source For IBM i 7.6 Information
  • Maxava Consulting Services Does More Than HA/DR Project Management – A Lot More
  • Guru: Creating An SQL Stored Procedure That Returns A Result Set
  • As I See It: At Any Cost
  • IBM i PTF Guide, Volume 27, Number 19

Content archive

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

Recent Posts

  • Big Blue Is Still Talking About Future Power Processors, Which Is Good
  • Who To Consult With On Your Cloud Strategy, And Who To Manage It
  • Guru: DateTime Rules Of Thumb
  • i-Rays Performance Analyzer Now Ready for Prime Time, Omniology Says
  • CNX Adds AI To Valence Development Tool
  • Q&A With IBM’s New GM Of Power, Hillery Hunter
  • When IBM i Skills Become A Resilience Risk
  • Guru: Load A Varying-Dimension Array With One SQL Fetch
  • You Have To Speak IBM’s Language If You Want To Be Heard
  • Raz-Lee Revs iSecurity Suite With 2026 Updates

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