how to break the loop in db2 database

别等时光非礼了梦想. 提交于 2019-12-25 20:45:05

问题


i have created 1 procedure and used 1 cursor, using this cursor i have used to loop but that loop fetching from cursor is going to infinite loop. below my code for procedure.

    db2 "CREATE OR REPLACE PROCEDURE NEWEMPSELECT7 RESULT SETS 1 LANGUAGE SQL 
BEGIN 
DECLARE OUTRATE VARCHAR(50);
DECLARE C1 CURSOR for 
SELECT Emp_name FROM NEWEMP WHERE Emp_id=100;
OPEN C1;
LOOP
FETCH FROM C1 INTO OUTRATE;
CALL DBMS_OUTPUT.PUT_LINE(OUTRATE);
END LOOP;
CLOSE C1;
END" 

The result is going to infinite loop. even i am not able to use C1%NOTFOUND or any keyword, because db2 doesnt support any keyword to break the rule.

please suggest how to break this loop in db2.


回答1:


If you are learning SQL PL, it will help you if you spend time studying the IBM example programs and making them work for your environment.

Here is a trivial example based on your code, that works with the SAMPLE database on Db2-LUW:

Note that there are more elegant ways to code such loops, including using the WHILE statement, using SQLSCODE/SQLSTATE variables and comparisons of these etc.

--#SET TERMINATOR @

SET SERVEROUTPUT ON@

CREATE OR REPLACE PROCEDURE NEWEMPSELECT7 
RESULT SETS 1 LANGUAGE SQL 
BEGIN 
    DECLARE OUTRATE VARCHAR(50);
    DECLARE v_at_end INTEGER default 0;
    DECLARE not_found CONDITION FOR SQLSTATE '02000';
    DECLARE C1 CURSOR for 
        SELECT LASTNAME FROM EMPLOYEE WHERE Empno=100;
    DECLARE CONTINUE HANDLER FOR not_found  SET v_at_end = 1 ;

    OPEN C1;
    fetch_loop:
    LOOP
        FETCH FROM C1 INTO OUTRATE;
        IF v_at_end <>0 THEN LEAVE fetch_loop; END IF;
        CALL DBMS_OUTPUT.PUT_LINE(OUTRATE);
    END LOOP;
    CLOSE C1;
END
@



回答2:


No matter the language, when I create a loop that is potentially infinite, I always build an "emergency exit" into it. I've done this in C#, VB.Net, VBA, JAVA, and just now for the first time in an IBM DB2 SQL function.

Since the concept is valid in any language, I'll present it in pseudocode, as follows:

Begin Procedure

Declare Variable safety_counter As Integer

Begin loop
    ...
    <body of code>
    If some_criteria = True Then          <-- this is your normal exit
        Exit Loop
    End If
    ...
    safety_counter = safety_counter + 1
    If safety_counter >= 1000             <-- set to any value you wish
        Exit Loop                         <-- this is the emergency exit
    End If
End Loop

Some variations are as follows.

If the loop is simple, the two exit criteria might be written in the same If-Then:

    If some_criteria = True Then          <-- normal exit
    Or safety_counter >= 1000             <-- emergency exit
        Exit Loop
    End If
    safety_counter = safety_counter + 1

An error value might be returned like this:

Begin Procedure

Declare Variable result_value   As Integer
Declare Variable safety_counter As Integer

Begin loop
    ...
    <body of code>
    If some_criteria = True Then          <-- this is your normal exit
        Set result_value = abc * xyz      <-- the value you seek          
        Exit Loop
    End If
    ...
    safety_counter = safety_counter + 1
    If safety_counter >= 1000             <-- set to any sufficient value
        Set result_value = (-123)         <-- indicate "infinite loop error"
        Exit Loop                         <-- this is the emergency exit
    End If
End Loop
Return result_value

There are many possible ways to do this, but the key is in safety_counter, and the way of monitoring it to trigger the emergency exit.



来源:https://stackoverflow.com/questions/53538273/how-to-break-the-loop-in-db2-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!