PL/SQL控制结构

<!-- [if gte mso 9]><xml><w:WordDocument><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:DocumentKind>DocumentNotSpecified</w:DocumentKind><w:DrawingGridVerticalSpacing>7.8</w:DrawingGridVerticalSpacing><w:View>Normal</w:View><w:Compatibility></w:Compatibility><w:Zoom>0</w:Zoom></w:WordDocument></xml><![endif]-->

一. 顺序结构

顺序结构就是按照语句出现的先后顺序执行


二. 选择结构

选择结构即if 结构,通过判断条件为 true 或为 false 来执行相应的代码,有下面 3 种结构

1. if-then

这个结构只有一个测试条件,如果条件为true 则执行 then 部分的代码,如果条件为 false 则跳过此结构,下面是一个实例:

declare

var1constantint:=50;

var2constantint:=200;

ifvar1>var2then

dbms_output.put_line('var1比 var2');

endif;

嵌套if-then 结构

declare

var1constantint:=50;

var2constantint:=200;

var3constantint:=120;

ifvar1>var2then

ifvar1>var3then

dbms_output.put_line('这三个数中 var1 最大 ');

endif;

endif;

❤注意

a.每个 if 语句都有自己的 then ,以 if 语句开始的语句行不跟语句结束符号“ ; ”。

b.每个 if 语句块以相应的 endif 结束。

2.if-then-else

此语句是在上一个if-then 的基础上加上一个 else 部分来处理当 if 条件为假时的语句。

if-then-else也可以嵌套使用,用于处理更为复杂的条件。

例如:

ifvar1>var2then

dbms_output.put_line('var1比 var2');

else

ifvar1=var2then

dbms_output.put_line('var1和 var2 一样大 ');

else

dbms_output.put_line('var1小于 var2');

endif;

endif;

c.每个 if 语句有且只有一个 else

d.else语句后面不跟语句结束符号。

3.if-then-elseif

用于替代嵌套if-then-else 结构,上面的例子可改写为:

ifvar1>var2then

dbms_output.put_line('var1比 var2');

elseifvar1=var2then

dbms_output.put_line('var1和 var2 一样大 ');

else

dbms_output.put_line('var1小于 var2');

endif;

e.elseif无匹配的 endif


三. NULL结构

NULL结构也叫空操作或空值处理,特别是在使用 if 逻辑结构时,用户测试一个条件,当测试条件为真,什么工作都不做,当测试条件为假,则执行某些操作,例如

ifvar1<60then

null;

else

insertintostuentCoursevarlues('pass',studentNo,courseNo);

endif;


四. 循环结构

Oracle提供三种循环结构

1. loop-exit-end

语法:

Loop

执行语句1

执行语句2

......

Exit

End;

实例:

setserveroutputon

declarectrinteger:=0;

begin

dbms_output.put(ctr||'');

ctr:=ctr+1;

exitwhenctr=10;

endloop;

dbms_output.put_line(loopexited');

end;

2. while-loop-end

语法:

While布尔表达式 loop

执行语句1

执行语句2

......

Endloop;

实例:

setserveroutputon

declarectrinteger:=0;

begin

whilectr<10loop

dbms_output.put(ctr||'');

ctr:=ctr+1;

endloop;

dbms_output.put_line(loopexited');

end;

3. for-in-loop-end

语法:

For循环变量 in[reverse] 起始值 .. 终止值 loop

执行语句1

执行语句2

...

Endloop;

Reverse关键字是标识循环倒过来执行,也就是循环变量从初始值一直减少到终止值。

实例:

1.输出 09 之间的数

setserveroutputon

begin

dbms_output.enable;

forctrin0..9loop

dbms_output.put(ctr||'');

endloop;

dbms_output.put_line('loopexited');

end;

2.循环打印阿斯克码表

setserveroutputonsize10240

SQL>declare

2inumber;

3jnumber;

4knumber;

5begin

6foriin2..15loop

7forjin1..16loop

8k:=i*16+j;

9dbms_output.put((to_char(k,'000'))||':'||chr(k)||'');

10ifkmod8=0then

11dbms_output.put_line('');

12endif;

13endloop;

14endloop;

15end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值