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;


 

發佈了32 篇原創文章 · 獲贊 2 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章