Procedure cflow
SQL PL contains a rich set of control flow statements a few of which are illustrated in the following procedure.
1 declare procedure cflow(i1 int, i2 int) 2 begin 3 declare ii, ij int; 4 if i1 < i2 then 5 set (ii, ij) = (i1, i1); 6 w_loop: while ii <= i2 do 7 set ij = ij * ii; 8 select i1, i2, ii, ij; 9 set ii = ii + 1; 10 if ii > 100 then 11 leave w_loop; 12 end if; 13 end while; 14 else 15 set (ii. ij) = (i2, i1); 16 r_loop: repeat 17 set ij = ij + ii; 18 select i1, i2, ii, ij; 19 set ii = ii + 1; 20 if ii > 100 then 21 leave r_loop; 22 end if; 23 until ii > i1; 24 end if; 25 end; 26 ;
Most of what happens in this procedure should be self-evident.
Notice that the assignment statement (SET) can be used to specify assignments to more than one variable at a time as in the SET statements at lines 5 and 15.
You may also notice that this procedure does not access a database.