您的位置:首页 > 数据库

pl/sql 之三大循环的完全学习指南

2013-01-22 15:38 351 查看
1 循环的概述
每个循环包括两个部分:循环边界和循环体。其中,循环边界由一些pl/sql保留字组成。位于循环体外部的代码不应该知道循环内部的工作。但是,循环是把双刃剑,程序的性能问题很容易定位到循环。

2 循环的类型

2.1 简单循环

语法:

[sql]
view plaincopyprint?

loop
可执行的语句
end loop;

loop
可执行的语句
end loop;


属性:

属性描述
why1)不能确定循环会执行多少次

2)要求循环至少执行一次

whenexit或者exit when发生时结束
how通过exit或者exit when来结束
注释:

1)离了exit或者exit when,简单循环便是死循环

2)exit when和exit的运用场景:

只有一个条件表达式决定循环是否应该结束时,使用exit when

[sql]
view plaincopyprint?

loop
balance_remaining := account_balance(account_id);
exit when balance_remaining<1000;

apply_balance(account_id,balance_remaining);
end loop;

loop
balance_remaining := account_balance(account_id);
exit when balance_remaining<1000;
apply_balance(account_id,balance_remaining);
end loop;


需要根据不同的退出条件来设置“返回值”时,在case语句中,使用exit

[sql]
view plaincopyprint?

loop
case
when salary>=10000
and salary<=20000
then
give_bonus(employee_id,1500);
exit;
when salary>2000
and salary<40000
then
give_bonus(employee_id,1000);
exit;
when salary>=40000

then
give_bonus(employee_id,500);
exit;
else
give_bonus(employee_id,0);
exit;
end case;

end loop;

loop
case
when salary>=10000 and salary<=20000
then
give_bonus(employee_id,1500);
exit;
when salary>2000 and salary<40000
then
give_bonus(employee_id,1000);
exit;
when salary>=40000
then
give_bonus(employee_id,500);
exit;
else
give_bonus(employee_id,0);
exit;
end case;
end loop;


2.2 while循环

语法:

[sql]
view plaincopyprint?

while condition
loop
可执行语句
end loop;

while condition
loop
可执行语句
end loop;


属性:

属性描述
why1)事先无法确定循环的次数

2)你想通过条件来终止循环

3)循环体不是必须要执行的

when条件判断是在循环边界处发生,每次进入循环体前都要进行这个判断
how循环边界的布尔表达式的求值结果是false 、null
注释:

1)while循环是依赖于条件的,倘若condition是false或者null,那么控制权就交割不到循环体手上

例子:

[sql]
view plaincopyprint?

while mask_index <= mask_count
loop
begin
retval :=to_date(value_in,fmts(mask_index));
date_converted :=true;

exception
when OTHERS
then
mask_index :=mask_index+1;
end;
end loop;

while mask_index <= mask_count
loop
begin
retval :=to_date(value_in,fmts(mask_index));
date_converted :=true;
exception
when OTHERS
then
mask_index :=mask_index+1;
end;
end loop;


2.3 for循环

2.3.1 数值型for循环

语法:

[sql]
view plaincopyprint?

for loop_index in [reverse] lowest_number..highest_number

loop
可执行语句
end loop;

for loop_index in [reverse] lowest_number..highest_number
loop
可执行语句
end loop;


属性:

属性描述
why如果只想有限次数的执行循环,但又不想过早的退出
when循环索引超过循环上边界时
how数值型for循环只要达到范围区指定的循环次数,就会无条件结束
注释:

1)loop_index会自动、隐士的被pl/sql引擎用一个int型的局部变量声明

2)范围部分使用的表达式,当且仅当循环开始时被求值一次,然后在整个生命周期内有效

3)循环体内改变范围表达式使用的变量,对循环边界没任何作用

4)循环体内不要改变loop_index或者范围边界值,这是个相当不好的编程习惯

例子:

[sql]
view plaincopyprint?

for loop_index in 1..100

loop
if mod(loop_index,2)=0
then
calc_values(loop_index);
end if;
end loop;

for loop_index in 1..100
loop
if mod(loop_index,2)=0
then
calc_values(loop_index);
end if;
end loop;


2.3.2 游标型for循环

语法:

[sql]
view plaincopyprint?

for record in {cursor_name |
select子句}
loop
可执行语句
end loop;

for record in {cursor_name | select子句}
loop
可执行语句
end loop;


属性:

属性描述
why当要依次取出一个游标中的每一行记录并处理时
when每次循环体执行后,pl/sql引擎会执行一个取数据的操作,如果游标中的%NOTFOUND属性为TRUE时,循环便结束
how当游标中的所有记录都取出后,游标型for循环就无条件结束
注释:

1)record由pl/sql引擎隐士声明,请不要再显示的声明一个循环索引同名的record了

2)如果declare cursor时,select子句中一列是表达式,则必须为这个表达式指定别名

3)在循环体内访问游标记录的特定值是通过句点表示法

4)循环执行次数为游标所取的记录数

例子:

--动态重建索引

[sql]
view plaincopyprint?

declare
cursor ind is
select index_name from user_indexes;

begin
for cur in ind

loop
execute immediate
'alter index '||cur.index_name||' rebuild';

end loop;
end;

declare
cursor ind is select index_name from user_indexes;
begin
for cur in ind
loop
execute immediate
'alter index '||cur.index_name||' rebuild';
end loop;
end;


3 循环的标签

定义:循环的别名

语法:<<tag_name>>

作用:

1)使用tag可以明确的把循环的开头和结束绑定在一起,提高了嵌套循环的可读性

2)让循环的index_loop更具规范化,无论是一条记录还是一个值

例子:

[sql]
view plaincopyprint?

<<year_loop>>
for year_number in 2012..2014

loop
<<month_loop>>
for month_number
in 1..12
loop
if year_loop.year_number=2012
then ..
end if;
end loop month_loop;
end loop year_loop;

<<year_loop>>
for year_number in 2012..2014
loop
<<month_loop>>
for month_number in 1..12
loop
if year_loop.year_number=2012
then ..
end if;
end loop month_loop;
end loop year_loop;


4 循环的技巧

1)index_loop使用可以自我说明的名称

2)从已关闭的游标中获取循环执行信息

例子:

[sql]
view plaincopyprint?

declare
book_count number :=0
for book_rec in book_cur (author_in =>
'THINK,WATER')
loop
..process data..
book_count := book_cur%ROWCOUNT;
end loop;
if book_count > 10
then ..

declare
book_count number :=0
for book_rec in book_cur (author_in => 'THINK,WATER')
loop
..process data..
book_count := book_cur%ROWCOUNT;
end loop;
if book_count > 10
then ..

原文地址:http://blog.csdn.net/linwaterbin/article/details/7885718
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: