Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

DB2 Get result set from nested stored procedure

I have a problem with db2. I'm new in this db. I am trying to get a result set from nested stored procedure but the result is "Query ok - 0 rows" :

Result of executed query

SP1 is:

 CREATE OR REPLACE PROCEDURE "SPSYSTEMMESSAGE" (IN pCode INTEGER) 
LANGUAGE SQL
SPECIFIC SQL210127173003073
DYNAMIC RESULT SETS 1

BEGIN
  DECLARE cursor1 CURSOR WITH RETURN TO CALLER FOR
  SELECT CODE, MESSAGE FROM SYSTEMMESSAGES WHERE CODE = pCode;
  OPEN cursor1;
 END

SP2 CALLER:

CREATE OR REPLACE PROCEDURE "SPADDINSTITUTION"
(IN param1 INTEGER)
LANGUAGE SQL


DYNAMIC RESULT SETS 1
 
BEGIN 

  IF (params IS NULL OR params = '') THEN 
       
               
    CALL SPSYSTEMMESSAGE(-1);  
  
  ELSE
        INSERT INTO TABLE VALUES (values...);
  END IF;
  
    
END

CALL SP2 : CALL SPADDINSTITUTION(''); RESULTS: OK. No rows were affected

I need to get the result set like this:

Result that I want

question from:https://stackoverflow.com/questions/65941265/db2-get-result-set-from-nested-stored-procedure

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your code cannot give the result you want, because your calling procedure (SPADDINSTITUTION) is failing to consume the result set returned by SPSYSTEMMESSAGE.

This is frequently asked, and you can find samples if you research.

Opening a cursor in the nested sproc is not enough, the calling sproc must consume the result-set.

Learn about these statements, all of which must appear in your calling procedure:

You have not explained how exactly the caller procedure should act on the result set, but that is a separate matter from consuming the result set.

You might want to pipeline the fetched result-set rows (one by one, with the PIPE() statement) to pass to the client program, we don't know, as you have not made it clear.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...