PL/SQL控制循环结构

常用的函数:

nvl (变量,0) ---如果变量值为null则取0,反之取变量本身的值

       lower()---将字母转为大写

1 条件控制

①IF语句

语法形式如下:

 

if    条件1    then

...执行语句

elsif        条件2     then

 ...执行语句

Else

 如果条件1,2 都不成立就执行这里的语句..

end if;

②case语句和case表达式

       1)单一选择符等值比较

语法如下:

case selector(选择项)

       whenexpression1 then sequence_of_statement1; --epression 表示指定条件值的表达式,sequence-of_statement 用于指定要执行的条件操作.

       whenexpression2 then sequence_of_statement3;

       …

       whenexpression then sequence_of_statementN;

end case;

       2)多种条件进行非等值比较

语法如下:

Case

       when指定的条件1     then 用于指定满足当前特定条件要执行的操作;

       …

when 指定的条件n      then 用于指定满足当前特定条件要执行的操作;

end case;

       3)case表达式

       形式1:

declare
              v_numnumber : = 2;
              v_charvarchar2(50);
       begin
              v_char:= case v_num
                                   when1 then ‘one’;
                                   when2 then ‘two’;
                                   else‘other’;
                            end;
       dbms_output_put_line(v_char);
end;

       形式 2:

    

   declare
              salaryNUMBER := 2000;
              bonus_amoutNUMBER;
       begin
              bonus_amount:=
                     case
                     whensalary >=10000 and salary <=20000 then 1500
                     whensalary >20000 and salary <= 40000 then 1000
                     whensalary > 40000 then 500
                     else0
                     end* 10;
              dbms_output.put_line(bonus_amout);
       end;

2循环控制

①基本的循环

       语法如下:

Loop

       Statement1;

Exit [when condition];

End loop;

while循环

语法如下:

While condition Loop

       Statement1;

       Statement2;

End loop;

For循环

For couter in[reverse]lower_bound ..upper_bound loop

Statement1;

End loop;

 

3顺序控制

①     goto语句;

②     null语句: null语句不会执行任何操作,并且会直接将控制传递到下一条语句.使用null语句的主要好处是可以提高PL/SQL程序的可读性;

goto:

Declare
       i int :=1;
begin
loop
insert into tempvalues(i);
if i=10 then
goto end_loop;
end if;
i:=i+1;
end loop;
<<end_loop>>
Dams_output.put_line(‘循环结束’);
End;


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值