• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Control The Flow Of Stored Procedure Result Sets

    July 24, 2013 Hey, Mike

    I have an RPG program (defined as an SQL external stored procedure) that returns a data structure array as a result set. When this procedure is called from iNavigator, I can see the result set. But in our .NET client/server environment, the result set is not returned to the .NET program. Further, the .NET program first calls a “setup” stored procedure that establishes the library list and general environment settings. Thereafter this “setup” procedure calls the RPG program that returns the result set. The RPG program runs upon request from the .NET world but no result set is returned.

    –B.C.

    Dear B.C.,

    The answer to this puzzling behavior can be found by reviewing the SET RESULT SETS statement. When returning a result set from a stored procedure (whether the source is an array or open cursor), the SET RESULT SETS statement specifies whether the result set is intended to be consumed by the client (i.e., the outermost procedure or caller in the call stack) or the caller (the prior procedure in the call stack). These two options are known as WITH RETURN TO CLIENT and WITH RETURN TO CALLER.

    Figure 1 below shows the options when returning a result set with multiple call stack entries. Incidentally, the default is WITH RETURN TO CALLER.

    Figure 1–Procedure call stack example with possible result set disposition.

    In this illustration, there is a sequence of four procedures. The last procedure in the stack (“Procedure D”) returns a result set. If “Return to Client” is specified then “Procedure A” can consume the opened result set. If “Return to Caller” is specified then “Procedure C” can consume the opened result set. Notice that there is no way that “Procedure B” can consume the result set returned by “Procedure D.” So, in DB2 for i, the result set can be returned to the prior caller or the primary caller but not to procedures in between.

    Now moving to your problem, the call stack looks something like Figure 2:

    Figure 2–Illustration of result set availability in a client/server environment.

    Remembering that the default is WITH RETURN TO CALLER, when the .NET program attempts to call the RPG stored procedure via the intermediate “setup” procedure, the RPG program makes the result set available to the “Setup” procedure. However, by changing the RPG program to use the “WITH RETURN TO CLIENT” option on the SET RESULT SETS statement, the RPG procedure will return the result set to the .NET caller.

    Usually, this isn’t a problem because if the .NET program called the RPG procedure directly, either option (CALLER/CLIENT) would work. However, it’s when you start nesting procedure calls that things can get thorny.

    If you’re having difficulty envisioning how this works, the following stored procedure MY_RESULTS will open a cursor and return the result set to either the caller or client (based on a specified parameter value).

    CREATE OR REPLACE PROCEDURE DEV.MY_RESULTS
    (@RETURN_TO_CLIENT CHAR(1))
    LANGUAGE SQL
    RESULT SETS 1
    SET OPTION COMMIT=*NONE
    BEGIN
    	DECLARE MY_DATA INSENSITIVE CURSOR 
    WITH RETURN TO CLIENT FOR
    	SELECT *
    	FROM QIWS.QCUSTCDT
    	;
    
    	OPEN MY_DATA;
    
    	IF @RETURN_TO_CLIENT='Y' THEN
        	    SET RESULT SETS 
    WITH RETURN TO CLIENT
    CURSOR MY_DATA;
    	ELSE
        SET RESULT SETS 
    WITH RETURN TO CALLER
    CURSOR MY_DATA;
    	END IF;
    END
    ;
    

    This procedure by default declares that its cursor will be returned to the client (i.e., the outermost caller whether a DB2 procedure, ADO.NET, JDBC, or OLE DB request). However, one of two possible SET RESULT SET statements are issued that can either keep the result set as available to the client or optionally override the result set destination so the caller (i.e., the prior procedure) can access it.

    In conclusion, an SQL result set opened in a stored procedure can be consumed by the caller (prior call stack) or the client (initiating program on the call stack). A procedure’s result sets are not available to other procedures in the call stack that fall “in between” these two options. If you have this situation arise, I’d suggest adding a parameter to dump the result set to a temporary table so that any procedure in between could pick up the result. Finally, SET RESULT SETS can be used to conditionally override the default behavior so that a procedure can make its result set available to either the “caller” or the “client.”

    Michael Sansoterra is a DBA for Broadway Systems in Grand Rapids, Michigan. Send your questions or comments for Mike via the IT Jungle Contact page.



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    Profound Logic Software:  Now On-Demand: Take IBM i EVERYWHERE With Mobile Apps!
    System i Developer:  Upgrade your skills at the RPG & DB2 Summit in Minneapolis, Oct 15-17.
    Abacus Solutions:  Qualified IBM i users eligible for free pair of running shoes

    More IT Jungle Resources:

    System i PTF Guide: Weekly PTF Updates
    IBM i Events Calendar: National Conferences, Local Events, and Webinars
    Breaking News: News Hot Off The Press
    TPM @ The Reg: More News From ITJ EIC Timothy Prickett Morgan

    Working Vacations On The Rise For Americans BlueFountain Delivers a Modern Looking WMS for IBM i

    Leave a Reply Cancel reply

Volume 13, Number 14 -- July 24, 2013
THIS ISSUE SPONSORED BY:

WorksRight Software
SEQUEL Software
ASNA

Table of Contents

  • Control The Flow Of Stored Procedure Result Sets
  • Value An Expression? *YES!
  • Automatically Answering IBM i Unable To Allocate Record Messages

Content archive

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

Recent Posts

  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • 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

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