一、條件分支語句
條件分支語句用于依據特定的情況選擇要執行的操作,PL/SQL提供了三種條件分支語句:if-then, if-then-else,if-then-elsif。
語法如下:
- if conditions then
- statements;
- [elseif conditions then
- statements;]
- [else
- statements;]
- end if;
if conditions then
statements;
[elseif conditions then
statements;]
[else
statements;]
end if;
1、if-then示例
用于執行單一條件判斷,如果滿足特定條件則會執行相應操作,如果不滿足特定條件則退出條件分支語句。
-
- declare
- v_count number;
- begin
- select count(*) into v_count from cip_temps;
- if(v_count>0) then
- dbms_output.put_line('v_cont的值:'|| v_count);
- end if;
- end;
- /
declare
v_count number;
begin
select count(*) into v_count from cip_temps;
if(v_count>0) then
dbms_output.put_line('v_cont的值:'|| v_count);
end if;
end;
/
2、if-then-else示例
用于執行二重條件判斷,如果滿足特定條件則執行一組操作,如果不滿足則執行另一組操作。
- declare
- v_count number;
- begin
- select count(*) into v_count from cip_temps;
- if(v_count>11) then
- dbms_output.put_line('v_cont的值:'|| v_count);
- else
- dbms_output.put_line('v_count的值:'|| v_count);
- end if;
- end;
- /
declare
v_count number;
begin
select count(*) into v_count from cip_temps;
if(v_count>11) then
dbms_output.put_line('v_cont的值:'|| v_count);
else
dbms_output.put_line('v_count的值:'|| v_count);
end if;
end;
/
3、if-then-elsif示例
用于執行多重條件判斷,如果滿足特定條件1則執行第一組操作,如果滿足特定條件2則執行第二組操作,以此類推,如果都不滿足特定條件則執行不滿足條件的操作。
- declare
- v_count number;
- begin
- select count(*) into v_count from cip_temps;
- if(v_count>10) then
- dbms_output.put_line('if操作___v_cont的值:'|| v_count);
- elsif (v_count=10) then
- dbms_output.put_line('elsif操作____v_count的值:'|| v_count);
- else
- dbms_output.put_line('else操作____v_cout的值:'||v_count);
- end if;
- end;
- /
declare
v_count number;
begin
select count(*) into v_count from cip_temps;
if(v_count>10) then
dbms_output.put_line('if操作___v_cont的值:'|| v_count);
elsif (v_count=10) then
dbms_output.put_line('elsif操作____v_count的值:'|| v_count);
else
dbms_output.put_line('else操作____v_cout的值:'||v_count);
end if;
end;
/
二、case語句
當執行多重條件分支語句時,使用case語句更加簡潔、而且效率也更好。case語句處理多重條件分支語句有兩種方法,第一種方法是使用單一選擇符進行等值比較。第二種方法是使用多種條件進行非等值比較。
1、使用單一選擇符進行等值比較
當執行case語句執行多重條件分支時,如果條件選擇符完全相同,并且條件表達式為相同條件選擇,那么可以選擇單一選擇符進行等值比較,語法如下:
- case 條件選擇符
- when 條件值表達式1 then 要執行的操作1;
- when 條件值表達式2 then 要執行的操作2;
- 。。。。。。。
- else
- 要執行的操作。
- end case;
case 條件選擇符
when 條件值表達式1 then 要執行的操作1;
when 條件值表達式2 then 要執行的操作2;
。。。。。。。
else
要執行的操作。
end case;
示例如下:
- declare
- v_count number;
- begi
- select count(*) into v_count from cip_temps;
- case v_count
- when 1 then
- dbms_output.put_line('when 1操作___v_cont的值:'|| v_count);
- when 5 then
- dbms_output.put_line('when 5操作___v_count的值:'|| v_count);
- when 10 then
- dbms_output.put_line('when 10操作____v_count的值:'|| v_count);
- else
- dbms_output.put_line('else操作____v_cout的值:'||v_count);
- end case;
- end;
- /
declare
v_count number;
begi
select count(*) into v_count from cip_temps;
case v_count
when 1 then
dbms_output.put_line('when 1操作___v_cont的值:'|| v_count);
when 5 then
dbms_output.put_line('when 5操作___v_count的值:'|| v_count);
when 10 then
dbms_output.put_line('when 10操作____v_count的值:'|| v_count);
else
dbms_output.put_line('else操作____v_cout的值:'||v_count);
end case;
end;
/
2、case使用多種條件進行比較
如果選擇多個條件進行不同比較時,那么必須在when子句中指定比較條件,語法如下:
- case
- when 條件值表達式1 then 要執行的操作1;
- when 條件值表達式2 then 要執行的操作2;
- 。。。。。。。
- else
- 要執行的操作。
- end case;
case
when 條件值表達式1 then 要執行的操作1;
when 條件值表達式2 then 要執行的操作2;
。。。。。。。
else
要執行的操作。
end case;
示例如下:
- declare
- v_count number;
- begin
- select count(*) into v_count from cip_temps;
- case
- when v_count>10 then
- dbms_output.put_line('when 1操作___v_cont的值:'|| v_count);
- when v_count>5 then
- dbms_output.put_line('when 5操作___v_count的值:'|| v_count);
- when v_count>4 then
- dbms_output.put_line('when 10操作____v_count的值:'|| v_count);
- else
- dbms_output.put_line('else操作____v_cout的值:'||v_count);
- end case;
- end;
- /
declare
v_count number;
begin
select count(*) into v_count from cip_temps;
case
when v_count>10 then
dbms_output.put_line('when 1操作___v_cont的值:'|| v_count);
when v_count>5 then
dbms_output.put_line('when 5操作___v_count的值:'|| v_count);
when v_count>4 then
dbms_output.put_line('when 10操作____v_count的值:'|| v_count);
else
dbms_output.put_line('else操作____v_cout的值:'||v_count);
end case;
end;
/
三、循環語句
三種循環:基本循環、while循環、for循環語句。
1、基本循環
循環語句以loop開始,以end loop結束,其語法如下:
loop
statement1;
。。。。。。
exit[when condition];
end loop;
注意:當執行基本循環語句時,無論是否滿足條件,語句至少會被執行一次,當condition為true時,會推出循環。并執行end loop后的相應操作。當編寫基本循環語句時,一定要有exit語句,否則會出現死循環,另外還要定義循環控制變量。
示例如下:
- declare
- i int:=1;
- begin
- loop
- dbms_output.put_line(i);
- exit when i=10;
- i:=i+1;
- nd loop;
- end;
- /
declare
i int:=1;
begin
loop
dbms_output.put_line(i);
exit when i=10;
i:=i+1;
end loop;
end;
/
2、while循環
對于while循環,只要條件為true時,才執行循環體內語句。while循環以while...loop開始,以end loop結束。其語法如下:
while 條件 loop
語句1;
語法2;
end loop;
當條件為true執行循環體內語句,當條件為false或full時,則跳出循環執行end loop以后的語句,另外還要定義循環控制變量。
示例如下:
- declare
- i int:=1;
- begin
- while i<=10 loop
- dbms_output.put_line(i);
- i:=i+1;
- end loop;
- end;
- /
declare
i int:=1;
begin
while i<=10 loop
dbms_output.put_line(i);
i:=i+1;
end loop;
end;
/
3、for循環
當使用for循環時,oracle會隱藏定義循環控制變量(即不需要人工定義循環控制變量),語法如下:
for counter in[reverse]
lower_bound..upper_bound loop
statement1;
statement2;
..........
end loop;
注意:counter循環控制變量,oracle會隱藏定義,lower_bound和upper_bound分別對應于循環變量的下界值和上界值,默認情況下,當使用for循環時,每次循環時循環控制變量會自動增一;如果指定reverse選項,那么每次循環時循環變量會自動減一。示例如下:
- declare
- begin
- for i in reverse 1..10 loop
- dbms_output.put_line(i);
- end loop;
- end;
- /
declare
begin
for i in reverse 1..10 loop
dbms_output.put_line(i);
end loop;
end;
/