Oracle之PL/SQL編程_流程控制語句

選擇語句sql


1. if...then 語句express

語法:ide

if < condition_expression > then
plsql_sentence
end if;


condition_expression:表示一個條件表達式,其值爲 true 時,程序會執行 if 下面的 PL/SQL 語句;oop

若是其值爲 false,則程序會跳過if 下面的語句而 直接執行 end if 後邊的語句。spa


plsql_sentence:condition_expression 爲 true 時,要執行的語句。server


2. if...then...else 語句it

語法:io

if < condition_expression > then
plsql_sentence_1;
else
plsql_sentence_2;
end if;


3.if...then...elsif 語句class

語法:變量

if < condition_expression1 > then
plsql_sentence_1;
elsif < condition_expression2 > then
plsql_sentence_2;
...
else
plsql_sentence_n;
end if;


4. case 語句

語法:

case < selector >
when <expression_1> then plsql_sentence_1;
when <expression_2> then plsql_sentence_2;
...
when <expression_n> then plsql_sentence_n;
[else plsql_sentence;]
end case;


selector:一個變量,用來存儲要檢測的值,一般稱之爲選擇器。

該選擇器的值須要與 when 子句中的表達式的值進行匹配。


expression_1:第一個 when 子句中的表達式,這種表達式一般是一個常量,當選擇器的值等於該表達式的值時,

程序將執行 plsql_setence_1 語句。


expression_2:第二個 when 子句中的表達式,這種表達式一般是一個常量,當選擇器的值等於該表達式的值時,

程序將執行 plsql_setence_2 語句。


expression_n:第 n 個 when 子句中的表達式,這種表達式一般是一個常量,當選擇器的值等於該表達式的值時,

程序將執行 plsql_setence_n 語句。


plsql_sentence:一個 PL/SQL 語句,當沒有與選擇器匹配的 when 常量時,程序將執行該 PL/SQL 語句,

其所在的 else 語句是一個可選項。


例:

指定一個季度數值,而後使用 case 語句判斷它所包含的月份信息並輸出。

代碼:

declare
season int:=3;
aboutlnfo varchar2(50);
begin
case season
when 1 then
aboutlnfo := season||'季度包括1,2,3 月份';
when 2 then
aboutinfo := season||'季度包括4,5,6 月份';
when 3 then
aboutinfo := season||'季度包括7,8,9 月份';
when 4 then
aboutinfo := season||'季度包括10,11,12 月份';
else
aboutinfo := season||'季節不合法';
end case;
dbms_output.put_line(aboutinfo);
end;


結果:3季度包括7,8,9 月份


循環語句


1. loop 語句

語法:

loop
plsql_sentence;
exit when end_condition_exp
end loop;

plsql_sentence:循環體中的PL/SQL 語句。至少被執行一遍。

end_condition_exp:循環結束條件表達式,當該表達式爲 true 時,則程序會退出循環體,不然程序將再次執行。


例:

使用 loop 語句求得前 100 個天然數的和,並輸出到屏幕。

SQL> set serveroutput on;
SQL> declare
sun_i int:=0;
i int:=0;
begin
loop
i:=i+1;
sum_i:=sum_i +1;
exit when i =100;--當循環 100次,程序退出循環體。
end loop;
dbms_output.put_line('前100個天然數和:'||sum_i);
end;
/


2. while 語句


語法:

while condition_expression loop
plsql_sentence;
end loop;


condition_expression: 表示一個條件表達式,但其值爲 true 時,程序執行循環體。

不然 程序退出循環體,程序每次執行循環體以前,都判斷該表達式是否爲 true。

plsql_sentence:循環內的plsql語句。


例:

使用while 語句求得 前100 個天然數的和,並輸出到屏幕。

declare 
sum_i int:=0;
i int:=0;
begin
while  i<=99 loop
 i:=i+1;
 sum_i:=sum_i+1;
end loop;
dbms_output.put_line('前100 個天然數的和是:'||sum_i);
end;
/


3. for 語句


語法:

for variable_counter_name in [reverse] lower_limit..upper_limit loop
plsql_sentence;
end loop;


variable_counter_name:表示一個變量,一般爲整數類型,用來做爲計數器。

默認狀況下 計數器的值會遞增,當在循環中使用 reverse 關鍵字時,計數器的值會隨循環遞減。


lower_limit:計數器下限值,當計數器的值小於下限值時,退出循環。

upper_limit:計數器上限值,當計數器的值大於上限值時,退出循環。

plsql_sentence:循環內的plsql語句。


例:

使用for語句求得前 100個天然數中偶數之和,並輸出到屏幕。

declare
sum_i int:= 0;
begin
for i in reverse 1..100 loop
if mod(i,2)=0 then--判斷是否爲偶數
 sum_i:=sum_i+i;
end if;
end loop;
dbms_output.put_line('前100個天然數中偶數和:'||sum_i);
end;
/
相關文章
相關標籤/搜索