stored-proceduresmainframedb2-zos

Need suggestion with Procedure code error in DB12 Z/OS


can someone help with my Proc code here? I am trying to execute this proc in DB12 Z/OS. Below is the body of my proc

CREATE PROCEDURE DEL_TBL_TEST23(IN TBL_NM VARCHAR(100))

DYNAMIC RESULT SETS 1


LANGUAGE SQL MODIFIES SQL DATA

BEGIN

DECLARE SQLCODE INTEGER;

DECLARE BDE_COUNTER INTEGER DEFAULT 0;

DECLARE V1 VARCHAR(50);

DECLARE V2 VARCHAR(100);

DECLARE V3 VARCHAR(100);

DECLARE V4 VARCHAR(100);

DECLARE V5 VARCHAR(200);

DECLARE V6 VARCHAR(500);

DECLARE T VARCHAR(500);

SET V1='DELETE FROM';

SET V2= TBL_NM;

SET V3='WHERE LN_NO IN (SELECT LN_NO FROM';

SET V4= TBL_NM;

SET V5='WHERE REC_CHNG_CD=''T''';                                       

SET V6='ORDER BY LN_NO FETCH FIRST 10000 ROWS ONLY)';

SET T = V1||V2||V3||V4||V5||V6 ;



DEL_LOOP:

    LOOP
    

    SET BDE_COUNTER=BDE_COUNTER + 1;

        EXECUTE IMMEDIATE T;

        COMMIT;
        

        IF SQLCODE = 100 THEN

            LEAVE DEL_LOOP; 

        END IF;    

    END LOOP DEL_LOOP;

    

COMMIT;

END

I want to delete some rows from a tablename, which I will provide while executing it. My proc here keeps on running in loops. Please help me know what am I doing wrong here?


Solution

  • Note that there is only one SQLCODE that always reflects the result of the last SQL-Statement. So instead of checking the SQLCODE from EXECUTE IMMEDIATE T you are seeing that of the COMMIT that will never be 100.

    So this might work:

    DEL_LOOP:
    
    LOOP
    
    
    SET BDE_COUNTER=BDE_COUNTER + 1;
    
        EXECUTE IMMEDIATE T;
    
    
        IF SQLCODE = 100 THEN
    
            LEAVE DEL_LOOP; 
    
        END IF;    
    
        COMMIT;
    
    END LOOP DEL_LOOP;
    
    COMMIT;