Returning result sets from SQL procedures is similar to returning result sets from external stored procedures. Client applications must use the CLI, JDBC, or SQLJ application programming interfaces to accept result sets from an SQL procedure. SQL procedures that call other SQL procedures also can accept result sets from those procedures. To return a result set from an SQL procedure, write your SQL procedure as follows:
For example, you can write an SQL procedure that returns a single result set, based on the value of the INOUT parameter threshold, as follows:
CREATE PROCEDURE RESULT_SET (INOUT threshold SMALLINT) LANGUAGE SQL DYNAMIC RESULT SETS 1 BEGIN DECLARE cur1 CURSOR WITH RETURN TO CALLER FOR SELECT name, job, years FROM staff WHERE years < threshold; OPEN cur1; END
If your application returns result sets from nested SQL procedures, you must use the WITH RETURN clause of the DECLARE CURSOR statement to ensure that DB2 returns the result sets to the appropriate location. If a target SQL procedure returns result sets to a calling SQL procedure, the caller must use the ALLOCATE CURSOR and ASSOCIATE RESULT SET LOCATOR statements to access and use the result set.
To always return a result set from an SQL procedure to a client application, use the WITH RETURN TO CLIENT clause in the DECLARE CURSOR statement associated with the result set. In the following example, the SQL procedure "CLIENT_SET" uses the WITH RETURN TO CLIENT clause in the DECLARE CURSOR statement to return a result set to the client application, even if "CLIENT_SET" is the target of a nested SQL procedure CALL statement:
CREATE PROCEDURE CLIENT_SET() DYNAMIC RESULT SETS 1 LANGUAGE SQL BEGIN DECLARE clientcur CURSOR WITH RETURN TO CLIENT FOR SELECT name, dept, job FROM staff WHERE salary > 20000; OPEN clientcur; END
To return a result set to the direct caller of an SQL procedure, whether the caller is a client application or another SQL procedure, use the WITH RETURN TO CALLER clause in the DECLARE CURSOR statement associated with the result set. In the following example, the SQL procedure "CALLER_SET" uses the WITH RETURN TO CALLER clause to return a result set to the caller of CALLER_SET:
CREATE PROCEDURE CALLER_SET() DYNAMIC RESULT SETS 1 LANGUAGE SQL BEGIN DECLARE clientcur CURSOR WITH RETURN TO CALLER FOR SELECT name, dept, job FROM staff WHERE salary > 15000; OPEN clientcur; END
When you expect your calling SQL procedure to receive a result set from a target SQL procedure, you must use the ALLOCATE CURSOR and ASSOCIATE RESULT SET LOCATOR statements to access and use the result set.
DECLARE result1 RESULT_SET_LOCATOR VARYING; DECLARE result2 RESULT_SET_LOCATOR VARYING; DECLARE result3 RESULT_SET_LOCATOR VARYING; CALL targetProcedure(); ASSOCIATE RESULT SET LOCATORS(result1, result2, result3) WITH PROCEDURE targetProcedure;
DECLARE result1 RESULT_SET_LOCATOR VARYING; DECLARE result2 RESULT_SET_LOCATOR VARYING; DECLARE result3 RESULT_SET_LOCATOR VARYING; CALL targetProcedure(); ASSOCIATE RESULT SET LOCATORS(result1, result2, result3) WITH PROCEDURE targetProcedure; ALLOCATE rsCur CURSOR FOR result1; WHILE (at_end = 0) DO SET total1 = total1 + var1; SET total2 = total2 + var2; FETCH FROM rsCur INTO var1, var2; END WHILE;