一、簡介
像程式語言一樣,oracle pl/sql也有自己的流程控制語句。通過流程控制語句,我們可以在pl/sql中實現一下比較複雜的業務邏輯操作。而無需到程式中去控制,在一定程度上提高了效率,這也是pl/sql的強大之處。pl/sql流程控制語句有如下幾種:
二、語句種類
1、控制語句
a、if語句
語法如下:
if 條件語句 tehn 執行語句
elsif 條件語句 tehn 執行語句
else 執行語句
end if;
示例如下:
declarei number
;
strvarchar2(18) :='b
';begin
--數字判斷
if(i =
6) then
null; --
如果什麼都不做建議寫null,保證語句的完整性
elsif(i is
null) then
dbms_output.put_line(
'null');
else
dbms_output.put_line(
'null');
endif
;
--字串判斷
if(str='
a') then
dbms_output.put_line('if
');elsif (
str='b
') then
dbms_output.put_line(
'elsif');
else
dbms_output.put_line(
'else');
endif
;end;
b、case語句
語法如下:
1、變數判斷
case 變數
when 值 then 執行語句
when 值 then 執行語句
end case;
2、表示式判斷
case
when 表示式 tehn 執行語句
when 表示式 tehn 執行語句
end case;
示例**如下:
declarestrvarchar2(18) :='b
';num
number :=60;
strresult
varchar2(10
); numresult
varchar2(10
);begin
--變數判斷
case
strwhen'a
'then strresult :='優秀
';when'b
'then strresult :='良好
';when'c
'then strresult :='合格
';when'd
'then strresult :=
'不及格';
endcase
;
--表示式判斷
case
when num <
60then numresult :=
'不及格';
when num <
70then numresult :='合格
';when num <
70then numresult :='良好
';when num <
70then numresult :='優秀
';endcase
;
dbms_output.put_line(strresult);
dbms_output.put_line(numresult);
end;
2、迴圈語句
a、loop語句
語法如下:
loop
執行語句
exit when 條件
end loop;
示例**:
declarei number :=0;
begin
loop
i := i +1;
dbms_output.put_line (i) ;
exit
when i =5;
endloop ;
end ;
b、while語句
語法如下:
while 條件 loop
執行語句
end loop;
--列印i每次增加後的值
declare
i number :=0;
begin
while i <
5loop
i := i +1;
dbms_output.put_line ('i:
'||i) ;
endloop ;
end ;
c、for語句
語法如下:
for 變數 in (reverse) 數字 .. 數字 loop
執行語句
end loop;
加上reverse就是數字的逆序。
示例**如下:
--index 是for開始就定了的
begin
--列印1到5之前的數
for i in
1 .. 5
loop
dbms_output.put_line ('i:
'||i) ;
endloop ;
--列印5-到20的數,逆序列印
for j in
reverse
5 .. 20
loop
dbms_output.put_line ('j:
'||j) ;
endloop ;
end ;
3、順序語句
a、goto語句
語法如下:
goto label;
goto語句一般都是由某行跳到另外一行。
示例**如下:
1declare
2 i number :=1;
3begin
4loop
5dbms_output.put_line (i) ;
6 i := i +1;
7if i = 10 then
8goto
label ;
9endif;
10end
loop ;
11<< label >>
12 dbms_output.put_line ('
label
') ;
13end;
本例子就i一直迴圈加一,當i等於10時,跳出迴圈,跳到第11行並執行。
Mysql之流程控制語句
這篇部落格主要是總結一下mysq中的流程控制語句的用法,主要是 case,if,ifnull,nullif 1.case case value when compare value then result when compare value then result else result end ...
shell 之 流程控制語句if
if 條件 then commands fi bin bash ifls shell then echo there is a dir named shell fi執行結果 注 根據我們命令退出的碼來進行判斷 是否為 0 如果是0,那麼就會執行then後面的命令 if 條件 then command...
Python之流程控制語句
一.流程控制之if else語句 if條件1 pass elif 條件2 pass elif 條件3 pass else pass 1.簡單的if列印 age of girl 31 if age of girl 30 print 阿姨好 2.if else 單分支 age of girl 31 if...