网站建设资讯

NEWS

网站建设资讯

OracleLOOP循环控制语句-创新互联

在PL/SQL中可以使用LOOP语句对数据进行循环处理,利用该语句可以循环执行指定的语句序列。常用的LOOP循环语句包含3种形式:基本的LOOP、WHILE...LOOP和FOR...LOOP。

LOOP语句的基本语法结构如下:
    [<>]

LOOP
statement...
END LOOP [label_name]

成都创新互联从2013年开始,是专业互联网技术服务公司,拥有项目做网站、网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元嘉兴做网站,已为上家服务,为嘉兴各地企业和个人服务,联系电话:13518219792

【语法说明】
<>:LOOP结构的标签,是可选项。
LOOP:LOOP循环开始标志。
statement:LOOP语句中循环执行的语句序列。
END LOOP:LOOP循环结束标志,可以添加LOOP结构的标签。
1.基本的LOOP语句
实例:要求声明变量,每次循环都需要为变量增加数字1,并输出结果。当变量值大于100时,退出循环操作。

a、利用EXIT...WHEN结束循环操作。

SQL> declare
2 v_rlt number(3):=1;
3 begin
4 <>
5 loop
6 dbms_output.put_line('v_rlt = '||v_rlt);
7 v_rlt:=v_rlt+1;
8 exit fst_loop when v_rlt > 100;
9 end loop;
10 dbms_output.put_line('LOOP循环已经结束!');
11 end;
12 /
v_rlt = 1
v_rlt = 2
v_rlt = 3
v_rlt = 4
v_rlt = 5
v_rlt = 6
v_rlt = 7
v_rlt = 8
v_rlt = 9
v_rlt = 10
v_rlt = 11
v_rlt = 12
v_rlt = 13
v_rlt = 14
v_rlt = 15
v_rlt = 16
v_rlt = 17
v_rlt = 18
v_rlt = 19
v_rlt = 20
v_rlt = 21
v_rlt = 22
v_rlt = 23
v_rlt = 24
v_rlt = 25
v_rlt = 26
v_rlt = 27
v_rlt = 28
v_rlt = 29
v_rlt = 30
v_rlt = 31
v_rlt = 32
v_rlt = 33
v_rlt = 34
v_rlt = 35
v_rlt = 36
v_rlt = 37
v_rlt = 38
v_rlt = 39
v_rlt = 40
v_rlt = 41
v_rlt = 42
v_rlt = 43
v_rlt = 44
v_rlt = 45
v_rlt = 46
v_rlt = 47
v_rlt = 48
v_rlt = 49
v_rlt = 50
v_rlt = 51
v_rlt = 52
v_rlt = 53
v_rlt = 54
v_rlt = 55
v_rlt = 56
v_rlt = 57
v_rlt = 58
v_rlt = 59
v_rlt = 60
v_rlt = 61
v_rlt = 62
v_rlt = 63
v_rlt = 64
v_rlt = 65
v_rlt = 66
v_rlt = 67
v_rlt = 68
v_rlt = 69
v_rlt = 70
v_rlt = 71
v_rlt = 72
v_rlt = 73
v_rlt = 74
v_rlt = 75
v_rlt = 76
v_rlt = 77
v_rlt = 78
v_rlt = 79
v_rlt = 80
v_rlt = 81
v_rlt = 82
v_rlt = 83
v_rlt = 84
v_rlt = 85
v_rlt = 86
v_rlt = 87
v_rlt = 88
v_rlt = 89
v_rlt = 90
v_rlt = 91
v_rlt = 92
v_rlt = 93
v_rlt = 94
v_rlt = 95
v_rlt = 96
v_rlt = 97
v_rlt = 98
v_rlt = 99
v_rlt = 100
LOOP循环已经结束!

PL/SQL procedure successfully completed.
b、利用IF...EXIT语句结束循环
SQL> declare
2 v_rlt number(3):=1;
3 begin
4 <>
5 loop
6 dbms_output.put_line('v_rlt = '||v_rlt);
7 v_rlt:=v_rlt+1;
8 if v_rlt > 100 then
9 dbms_output.put_line('变量的值已经大于100,当前值为'||v_rlt);
10 exit fst_loop;
11 end if;
12 end loop;
13 dbms_output.put_line('LOOP循环已经结束!');
14 end;
15 /
v_rlt = 1
v_rlt = 2
v_rlt = 3
v_rlt = 4
v_rlt = 5
v_rlt = 6
v_rlt = 7
v_rlt = 8
v_rlt = 9
v_rlt = 10
v_rlt = 11
v_rlt = 12
v_rlt = 13
v_rlt = 14
v_rlt = 15
v_rlt = 16
v_rlt = 17
v_rlt = 18
v_rlt = 19
v_rlt = 20
v_rlt = 21
v_rlt = 22
v_rlt = 23
v_rlt = 24
v_rlt = 25
v_rlt = 26
v_rlt = 27
v_rlt = 28
v_rlt = 29
v_rlt = 30
v_rlt = 31
v_rlt = 32
v_rlt = 33
v_rlt = 34
v_rlt = 35
v_rlt = 36
v_rlt = 37
v_rlt = 38
v_rlt = 39
v_rlt = 40
v_rlt = 41
v_rlt = 42
v_rlt = 43
v_rlt = 44
v_rlt = 45
v_rlt = 46
v_rlt = 47
v_rlt = 48
v_rlt = 49
v_rlt = 50
v_rlt = 51
v_rlt = 52
v_rlt = 53
v_rlt = 54
v_rlt = 55
v_rlt = 56
v_rlt = 57
v_rlt = 58
v_rlt = 59
v_rlt = 60
v_rlt = 61
v_rlt = 62
v_rlt = 63
v_rlt = 64
v_rlt = 65
v_rlt = 66
v_rlt = 67
v_rlt = 68
v_rlt = 69
v_rlt = 70
v_rlt = 71
v_rlt = 72
v_rlt = 73
v_rlt = 74
v_rlt = 75
v_rlt = 76
v_rlt = 77
v_rlt = 78
v_rlt = 79
v_rlt = 80
v_rlt = 81
v_rlt = 82
v_rlt = 83
v_rlt = 84
v_rlt = 85
v_rlt = 86
v_rlt = 87
v_rlt = 88
v_rlt = 89
v_rlt = 90
v_rlt = 91
v_rlt = 92
v_rlt = 93
v_rlt = 94
v_rlt = 95
v_rlt = 96
v_rlt = 97
v_rlt = 98
v_rlt = 99
v_rlt = 100
变量的值已经大于100,当前值为101
LOOP循环已经结束!

PL/SQL procedure successfully completed.
2、WHILE...LOOP结构
WHILE...LOOP结构和基本的LOOP语句不同,它本身可以结束LOOP循环。WHILE关键词后面需要有布尔表达式,当WHILE后面的布尔表达式为TRUE时,则循环体重的语句序列被执行1次,然后会重新判断WHILE后面的表达式是否为TRUE,只有当WHILE后的布尔表达式为FALSE时,才结束整个LOOP循环。

该语句结构的相关语法如下:
    [<>]

WHILE boolean_expression
LOOP
statement...
END LOOP [label_name];
【语法说明】
boolean_expression:布尔表达式。
statement:语句序列,当boolean_expression为TRUE时,该语句序列可获得执行权。
SQL> declare
2 v_rlt number(3):=1;
3 begin
4 <>
5 while( v_rlt < 100)
6 loop
7 dbms_output.put_line('v_rlt = '||v_rlt);
8 v_rlt:=v_rlt+1;
9 end loop;
10 dbms_output.put_line('while循环已经结束!');
11 end;
12 /
v_rlt = 1
v_rlt = 2
v_rlt = 3
v_rlt = 4
v_rlt = 5
v_rlt = 6
v_rlt = 7
v_rlt = 8
v_rlt = 9
v_rlt = 10
v_rlt = 11
v_rlt = 12
v_rlt = 13
v_rlt = 14
v_rlt = 15
v_rlt = 16
v_rlt = 17
v_rlt = 18
v_rlt = 19
v_rlt = 20
v_rlt = 21
v_rlt = 22
v_rlt = 23
v_rlt = 24
v_rlt = 25
v_rlt = 26
v_rlt = 27
v_rlt = 28
v_rlt = 29
v_rlt = 30
v_rlt = 31
v_rlt = 32
v_rlt = 33
v_rlt = 34
v_rlt = 35
v_rlt = 36
v_rlt = 37
v_rlt = 38
v_rlt = 39
v_rlt = 40
v_rlt = 41
v_rlt = 42
v_rlt = 43
v_rlt = 44
v_rlt = 45
v_rlt = 46
v_rlt = 47
v_rlt = 48
v_rlt = 49
v_rlt = 50
v_rlt = 51
v_rlt = 52
v_rlt = 53
v_rlt = 54
v_rlt = 55
v_rlt = 56
v_rlt = 57
v_rlt = 58
v_rlt = 59
v_rlt = 60
v_rlt = 61
v_rlt = 62
v_rlt = 63
v_rlt = 64
v_rlt = 65
v_rlt = 66
v_rlt = 67
v_rlt = 68
v_rlt = 69
v_rlt = 70
v_rlt = 71
v_rlt = 72
v_rlt = 73
v_rlt = 74
v_rlt = 75
v_rlt = 76
v_rlt = 77
v_rlt = 78
v_rlt = 79
v_rlt = 80
v_rlt = 81
v_rlt = 82
v_rlt = 83
v_rlt = 84
v_rlt = 85
v_rlt = 86
v_rlt = 87
v_rlt = 88
v_rlt = 89
v_rlt = 90
v_rlt = 91
v_rlt = 92
v_rlt = 93
v_rlt = 94
v_rlt = 95
v_rlt = 96
v_rlt = 97
v_rlt = 98
v_rlt = 99
while循环已经结束!

PL/SQL procedure successfully completed.
3.FOR...LOOP结构
FOR...LOOP语句可以遍历某个范围的整数,该范围被FOR和LOOP关键词封闭。首次进入循环时,循环范围将被确定,并且以后不会再次计算。每循环一次,循环指数将会自动增加1。

FOR...LOOP语句的语法结构如下
    [<>]

FOR index_name IN
[ REVERSE ]
lower_bound .. upper_bound
LOOP
statement...
END LOOP [label_name];
【语法说明】
index_name:循环计数器,是一个变量,它可以得到当前的循环指数。需要注意的是,不能为其手工赋值。
REVERSE:可选项,指定循环方式。默认的循环方式由下标(lower_bound)到上标(upper_bound)。使用该选项则从上标界到下标界。
lower_bound:循环范围的下标界。
upper_bound:循环范围的上标界。
下标和上标之间的".."不能省略。
SQL> declare
2 v_rlt number(3):=1;
3 begin
4 for v_rlt in 1..100 loop
5 dbms_output.put_line('v_rlt = '||v_rlt);
6 end loop;
7 dbms_output.put_line('for循环已经结束!');
8 end;
9 /
v_rlt = 1
v_rlt = 2
v_rlt = 3
v_rlt = 4
v_rlt = 5
v_rlt = 6
v_rlt = 7
v_rlt = 8
v_rlt = 9
v_rlt = 10
v_rlt = 11
v_rlt = 12
v_rlt = 13
v_rlt = 14
v_rlt = 15
v_rlt = 16
v_rlt = 17
v_rlt = 18
v_rlt = 19
v_rlt = 20
v_rlt = 21
v_rlt = 22
v_rlt = 23
v_rlt = 24
v_rlt = 25
v_rlt = 26
v_rlt = 27
v_rlt = 28
v_rlt = 29
v_rlt = 30
v_rlt = 31
v_rlt = 32
v_rlt = 33
v_rlt = 34
v_rlt = 35
v_rlt = 36
v_rlt = 37
v_rlt = 38
v_rlt = 39
v_rlt = 40
v_rlt = 41
v_rlt = 42
v_rlt = 43
v_rlt = 44
v_rlt = 45
v_rlt = 46
v_rlt = 47
v_rlt = 48
v_rlt = 49
v_rlt = 50
v_rlt = 51
v_rlt = 52
v_rlt = 53
v_rlt = 54
v_rlt = 55
v_rlt = 56
v_rlt = 57
v_rlt = 58
v_rlt = 59
v_rlt = 60
v_rlt = 61
v_rlt = 62
v_rlt = 63
v_rlt = 64
v_rlt = 65
v_rlt = 66
v_rlt = 67
v_rlt = 68
v_rlt = 69
v_rlt = 70
v_rlt = 71
v_rlt = 72
v_rlt = 73
v_rlt = 74
v_rlt = 75
v_rlt = 76
v_rlt = 77
v_rlt = 78
v_rlt = 79
v_rlt = 80
v_rlt = 81
v_rlt = 82
v_rlt = 83
v_rlt = 84
v_rlt = 85
v_rlt = 86
v_rlt = 87
v_rlt = 88
v_rlt = 89
v_rlt = 90
v_rlt = 91
v_rlt = 92
v_rlt = 93
v_rlt = 94
v_rlt = 95
v_rlt = 96
v_rlt = 97
v_rlt = 98
v_rlt = 99
v_rlt = 100
for循环已经结束!

PL/SQL procedure successfully completed.

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页题目:OracleLOOP循环控制语句-创新互联
文章链接:http://cdweb.net/article/hegid.html