Step 1: Create a table:
CREATE TABLE DEPT (DEPTNO CHAR(3) NOT NULL, DEPTNAME VARCHAR(36) NOT NULL, PRIMARY KEY(DEPTNO))
Step 2: Populate data into table:
INSERT INTO DEPT (DEPTNO, DEPTNAME) VALUES ('B11', 'PURCHASING'), ('E41', 'DATABASE ADMINISTRATION') ;
Step 3: Create SQL store procedure:
CREATE PROCEDURE TESTMULTIRS --We do not use this input parameter. (IN i_cmacct CHARACTER(5)) RESULT SETS 2 LANGUAGE SQL BEGIN DECLARE csnum INTEGER; --Declare serial cursors as serial cursor consume less resources --and we do not need rollable cursor. DECLARE getDeptNo CHAR(50); --Be careful about estimated length here. DECLARE getDeptName CHAR(200); DECLARE c1 CURSOR WITH RETURN FOR s1; DECLARE c2 CURSOR WITH RETURN FOR s2; SET getDeptNo = 'SELECT DEPTNO FROM DEPT'; SET getDeptName = 'SELECT DEPTNAME FROM DEPT'; PREPARE s1 FROM getDeptNo; OPEN c1; PREPARE s2 FROM getDeptName; OPEN c2; END;
Step 4: Call stored procedure in iNavigator:
call testmultirs('jia');
Then we can see outpu as below in inavigator:
and
Hi, Yiyu,
ReplyDeleteThanks for the example above. The technique of defining multiple result sets in a DB2 stored procedure can speed up IBM i database queries from PHP.
Keep sharing your discoveries,
Alan
Thanks.. it works great..
ReplyDeleteSuper... This was really Sweet and Simple example... Thanks a lot
ReplyDeleteThank You!
ReplyDeleteThis example helped me write my first ever Db2 procedure.
can you tell me steps to execute the above stored procedure in DB2 9.7 Control center ?
ReplyDeletei mean should i save the above procedure with .sql extension and then call it or directly write above code in the Control center.
THANKS FINALLY I EXECUTED THIS CODE TODAY !!
ReplyDeleteTHANKSSSSS
After creating the procedure how to ecexute that can you send the code for that.
DeleteThank You!
ReplyDeleteThis example helped me write my first ever Db2 procedure. :)
how to find count of rows fetched through cursor
ReplyDeleteWorks great was having a hard time finding a DB2 SP Simple Example
ReplyDeletehow to see the logic for already created procedure?
ReplyDelete