PL/SQL控制循环结构

来源:互联网 发布:软件版本说明模板 编辑:程序博客网 时间:2024/05/22 08:20

常用的函数:

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;beginloopinsert into tempvalues(i);if i=10 thengoto end_loop;end if;i:=i+1;end loop;<<end_loop>>Dams_output.put_line(‘循环结束’);End;


 

0 0
原创粉丝点击