REPEAT statement in SQL procedures

The REPEAT statement defines a set of statements to be executed until a condition that is evaluated at the end of the REPEAT loop is true.

The repeat-loop-condition is evaluated at the completion of each iteration of the loop.

With a WHILE statement, the loop is not entered if the while-loop-condition is false at 1st pass. The REPEAT statement is useful alternative; however it is noteworthy that while-loop logic can be rewritten as a REPEAT statement.

Here is an SQL procedure that includes a REPEAT statement:

  CREATE PROCEDURE sum_mn2 (IN p_start INT
                           ,IN p_end INT
                           ,OUT p_sum INT)
  SPECIFIC sum_mn2
  LANGUAGE SQL
  smn2: BEGIN
  
    DECLARE v_temp INTEGER DEFAULT 0;
    DECLARE v_current INTEGER;

    SET v_current = p_start;

    REPEAT
      SET v_temp = v_temp + v_current;
      SET v_current = v_current + 1;
    UNTIL (v_current > p_end)
    END REPEAT;
  END