Click here to Skip to main content
15,881,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Experts,

I am Query in which there should be a loop for each row of the results from the Query to pass a parameter to external Proc. Here it is without any error.

BEGIN 

DECLARE v_BIN CHAR(10);
DECLARE RESULT INTEGER DEFAULT 0 ; 

FOR v_row AS 

    SELECT T2 . ICITEM , T2 . BABLOC , C . ISSIZE , D . UNITSPER 
    
    , E . NBRRCKBINB , F . ICDSC1 , F . ICDSC2 , T2 . TQOH , T2 . TQCM 
    
    , CEIL ( INTEGER ( ( T2 . TQOH ) ) / D . UNITSPER ) 
    
    NBR_USED_RACKS 
    
    , E . NBRRCKBINB - ( CEIL ( INTEGER ( ( T2 . TQOH ) ) / D . UNITSPER ) ) 
    
    NBR_OPEN_RACKS
    
    FROM ( 
    
    SELECT A . BACMP , A . BALOC , MIN ( A . BAITEM ) ICITEM 
    
    , A . BABLOC , INTEGER ( SUM ( A . BAQOH ) ) TQOH , 
    
    INTEGER ( SUM ( A . BAQCM ) ) TQCM 
    
    FROM TESTDATA . VINBINI A WHERE A . BALOC = '13' AND 1 = A . BACMP AND 
    
    A . BAQOH - A . BAQCM > 0 GROUP BY A . BACMP , A . BALOC , A . BABLOC HAVING 
    
    MIN ( A . BAITEM ) = MAX ( A . BAITEM ) 
    
    AND SUM ( A . BAQOH - A . BAQCM ) > 0 
    
    ) AS T2 
    
    , TESTDATA . PALITMLOC B , TESTDATA . VINITEMSIZ C , TESTDATA . PALSIZQTY D , TESTDATA . PALBINPF E 
    
    , TESTDATA . VINITEM F 
    
    WHERE T2 . BACMP = B . TACOMP AND T2 . ICITEM = B . ICITEM 
    
    AND T2 . BALOC = B . IALOC AND T2 . ICITEM = F . ICITEM 
    
    AND T2 . ICITEM = C . ISITEM 
    
    AND B . PALLETID = D . PALLETID 
    
    AND C . ISSIZE = D . ISSIZE 
    
    AND E . TACOMP = T2 . BACMP 
    
    AND E . IALOC = T2 . BALOC 
    
    AND E . IMBLOC = T2 . BABLOC 
    
    AND E . PALLETID = B . PALLETID 
    
        ORDER BY 1 , 2	 
 
    DO	 
	 	 
        SET v_BIN = v_row.BABLOC;	                                               
   	call TESTDATA.PARECR24SP (  '01' , '13  ',v_BIN ,RESULT) ;                               
END FOR;

END;


Now I want the results to be printed/ shown in output window. Here it says just success message.

This is really a big thing now for me. I banging my head to the wall since last three days. please help
Posted

1 solution

The procedure you've created is designed to loop through the rows and call the sub program. If you want to return a result set you need to design it differently:
- use DYNAMIC RESULT SETS
- define a cursor
- open a cursor
- return

Have a look at the procedure definition in Example 1: Calling a stored procedure that returns a single result set[^]

ADDITION

What comes to the question if the query is correct or not I really can't say since I know nothing about the tables nor the logic.

If it would have syntactical problems they would show as errors but if it has logical errors and returns wrong rows or no rows at all, that's up to the statement logic.

Since you need to return the result to the calling side, have a test run with the following. That's part of your original query so it should return something meaningful.

One thing I noticed is that you were missing the keyword CURSOR...
SQL
CREATE PROCEDURE ResultTest () LANGUAGE SQL
 DYNAMIC RESULT SETS 1
 BEGIN
    DECLARE v_row CURSOR FOR 
    SELECT A.BACMP, 
           A.BALOC, 
           A.BAITEM,    
           A.BABLOC,
           A.BAQOH, 
           A.BAQCM
   FROM TESTDATA.VINBINI A 
   WHERE A.BALOC = '13' 
   AND   1 = A.BACMP 
   AND   A.BAQOH - A.BAQCM > 0;

    OPEN v_row;

    RETURN;
END
 
Share this answer
 
v2
Comments
sudevsu 30-Sep-15 14:37pm    
I did try this. But no luck. Can you just tell me or write the same. I am pretty new to DB2
Wendelius 30-Sep-15 15:01pm    
Have a look at the modified answer.
sudevsu 30-Sep-15 16:48pm    
This is not what I am looking for. I want the external proc to be called on each row with a parameter passing from select query
Wendelius 1-Oct-15 0:09am    
As far as I can see your original procedure already does that. You're just missing the cursor keyword from the beginning of the declaration (see the example I wrote).

If the procedure isn't working as expected, have you checked that the query really returns rows? Also if the result variable should get the value from the procedure being called check that you have defined the parameter as OUT or INOUT, depending on the situation. See CREATE PROCEDURE[^]
sudevsu 30-Sep-15 14:42pm    
Before that, Can you tell me if my query written above is correct ? Meaning, for each row of the select statement results, it should pass a value from it to external proc and return a result union of both the query and external proc.
I am hanging on the edge and banging my head since last three days for this. Google doesn't help me in finding any solutions in DB2.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900