REPEAT statement

The REPEAT loop is a mechanism for continuously looping through a section of logic. It’s similar to the WHILE construct, but the condition will be checked at the end of the construct. That means at least once the statement in the construct will be executed.

The construct starts with the REPEAT keyword. The condition will be checked with the UNTIL keyword at the construct’s end. The construct will be closed with the END REPEAT keyword.

The condition will be evaluated after the statements in the loop were executed. If the condition is true the loop is entered a next time.

Example (similar to the while-example, but with a repeat loop):

CREATE PROCEDURE DEL_DEPT (IN deldept VARCHAR(3))
LANGUAGE SQL
BEGIN ATOMIC
  DECLARE cursor_end integer default 0;
  DECLARE currentDEPT CHAR(3);
  DECLARE mycur CURSOR FOR
      select DEPTNO as v_dept
      from DEPARTMENT
      where deptno = deldept;
  DECLARE CONTINUE HANDLER FOR NOT FOUND
      set cursor_end = 1;

  OPEN mycur;
  REPEAT
      FETCH FROM mycur INTO currentDEPT;
      delete from EMPLOYEE where workdept = currentDEPT;
      UNTIL cursor_end = 1
  END REPEAT;
  CLOSE mycur;
  delete from DEPARTMENT where deptno = deldept;
END!

Link to the DB2 Information Center: DB2 9.7 DB2 10.1 DB2 10.5