sql loop in pl / sql

1. While loop

1. Grammar

declare 
    i number(2) := 1;--定义变量
begin
  while 条件判断 loop
        dbms_output.put_line(i);--输出语句
        i := i+1;--步进表达式
  end loop;

end;

2. Examples
image

3. Output results

image

2. Exit loop

1. Grammar

--方法二【exit循环】
declare
  i number(2) := 1;--定义变量
begin
  loop
    exit when i > 10;--退出循环条件
    dbms_output.put_line(i);--输出语句
    i := i+1;--步进表达式
  end loop;

end;

2. Examples
image

3. Output results

image

Three. For loop

1. Grammar

--方法三【for循环】
declare
--for循环的变量在for语句中定义
begin
  for 定义变量名 in 1..10 loop--其中1..10是步进表达式,包含头1和尾10
    dbms_output.put_line(i);
  end loop;--结束标记

end;

2. Examples
image

3. Output results

image

4. Summary

1 ... 10 is a number from 1 to 10, including 1 and 10.

end loop is the end-of-loop marker and must be written.

In addition to the for loop, the other two loops need to define the variable [declare inside].

Published 62 original articles · won praise 20 · views 6775

Guess you like

Origin blog.csdn.net/qq_45421186/article/details/105464014