• 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:

    • 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: Tags: 400guru, DB2 for i, FHG, IBM i, RPG, SQL, VS Code

    Sponsored by
    FalconStor

    Simplify Secure Offsite Data Protection for IBM Power with FalconStor Habanero™

    IBM i teams are under growing pressure to ensure data is protected, recoverable, and compliant—without adding complexity or disrupting stable environments.

    FalconStor Habanero™ provides secure, fully managed offsite data protection purpose-built for IBM Power. It integrates directly with existing IBM i backup tools and processes, enabling reliable offsite copies without new infrastructure, workflow changes, or added operational overhead.

    By delivering and managing the service end-to-end, FalconStor helps organizations strengthen cyber resilience, improve disaster recovery readiness, and meet compliance requirements with confidence. Offsite copies are securely maintained and available when needed, supporting recovery, audits, and business continuity.

    FalconStor Habanero offers a straightforward way to modernize offsite data protection for IBM i: focused on simplicity, reliability, and resilience.

    Learn More

    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

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

    Leave a ReplyCancel 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

  • Fall TRs Expected Next Week At Combined IBM-COMMON Conference
  • GenAI Brings A New Kind Of Agile Computing That Needs Controls Even More
  • IBM i, AI, And The Cost Of “We’ve Always Done It That Way”
  • Infor Says IBM i Customers Are Welcome To Use New AI
  • IBM i PTF Guide, Volume 28, Number 36
  • Help Us Understand The IBM i Marketplace By Taking The Survey
  • IBM Goes GA With Power Autonomous Operations
  • IBM Wheels And Deals To Sell Power Systems, Storage, and Software
  • Guru: When RNF0722 Won’t Let Your Legendary RPG Program Compile
  • A Read-Only MCP Server For Db2 for i

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