Until Loop In a Procedure

This page discusses - Until Loop In a Procedure

Until Loop In a Procedure

Until Loop In a Procedure

     

Until Loop In a Procedure is used to execute a set of statements repeatedly until a certain condition is reached.

Understand with Example

Many Beginners trapped with the problem of  execution of loop in a procedure. This Tutorial provides you an example to understand 'Until Loop In a Procedure'. To grasp this example, we create a procedure 'proce' that accept variable 'a' as input parameter. The data type specified for variable is integer type.

The set @ a is initialized to 0 initial.

The repeat loop does not evaluate the condition at the beginning ,unlike while loop. If the evaluate condition in the beginning of loop is false, the loop will be executed, until the final line of code in loop. 

 

Create Procedure

delimiter $$
create procedure proce(a int)
begin
	set @a = 0;
	repeat set @a = @a + 1; 
		select @a;
	
	until @a>a
	end repeat;
END$$
delimiter ;

Call Procedure

To call a procedure proce, we use a Call proce that execute the loop five times and return you the value into @. 

Call proce(5);

Result

+------+
| @a   |
+------+
| 1    |
+------+
1 row in set (0.00 sec)
+------+
| @a   |
+------+
| 2    |
+------+
1 row in set (0.03 sec)
+------+
| @a   |
+------+
| 3    |
+------+
1 row in set (0.08 sec)
+------+
| @a   |
+------+
| 4    |
+------+
1 row in set (0.11 sec)
+------+
| @a   |
+------+
| 5    |
+------+
1 row in set (0.11 sec)
+------+
| @a   |
+------+
| 6    |
+------+
1 row in set (0.11 sec)