1. loop语句sql
loopexpress
plsql_sentence;oop
exit when end_condition_exp;spa
end loop;code
loop语句会先执行一次循环体,而后再判断“exit when”关键字后面的条件表达式,若是条件表达式为true,则退出循环体,若是条件表达式为false,则继续执行循环体。 blog
declare -- Local variables here v_sum int := 0; v_i int := 0; begin -- Test statements here loop v_i := v_i + 1; v_sum := v_sum + v_i; exit when v_i = 100; end loop; dbms_output.put_line('1到100的和为:' || v_sum); end;
2. while语句
while condition_expression loopit
plsql_sentence;io
end loop;class
执行前判断条件表达式,如条件表达式为True,则执行循环体,如条件表达式为false,则退出循环体。变量
declare -- Local variables here v_sum int := 0; v_i int := 0; begin -- Test statements here while v_i < 100 loop v_i := v_i + 1; v_sum := v_sum + v_i; end loop; dbms_output.put_line('1到100的和为:' || v_sum); end;
3. for语句
for variable in [reverse] lower_limit .. up_limit loop
plsql_sentence;
end loop;
variable表示一个变量,一般为整数类型,用来做为计数器。
low_limit表示计算的下线值。当值小于下线值时,退出循环。
up_limit表示计算的上线值。当值大于上线值时,退出循环。
reverse表示计数器随循环递减。
declare -- Local variables here v_sum int := 0; begin -- Test statements here for v_i in reverse 1 .. 100 loop v_sum := v_sum + v_i; end loop; dbms_output.put_line('1到100的和为:' || v_sum); end;