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

來源:互聯網
上載者:User

標籤:oracle   for   while   

選擇語句


1. if...then 語句

文法:

if < condition_expression > thenplsql_sentenceend if;


condition_expression:表示一個條件運算式,其值為 true 時,程式會執行 if 下面的 PL/SQL 陳述式;

如果其值為 false,則程式會跳過if 下面的語句而 直接執行 end if 後邊的語句。


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


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

文法:

if < condition_expression > thenplsql_sentence_1;elseplsql_sentence_2;end if;


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

文法:

if < condition_expression1 > thenplsql_sentence_1;elsif < condition_expression2 > thenplsql_sentence_2;...elseplsql_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 語句判斷它所包含的月份資訊並輸出。

代碼:

declareseason int:=3;aboutlnfo varchar2(50);begincase seasonwhen 1 thenaboutlnfo := season||‘季度包括1,2,3 月份‘;when 2 thenaboutinfo := season||‘季度包括4,5,6 月份‘;when 3 thenaboutinfo := season||‘季度包括7,8,9 月份‘;when 4 thenaboutinfo := season||‘季度包括10,11,12 月份‘;elseaboutinfo := season||‘季節不合法‘;end case;dbms_output.put_line(aboutinfo);end;


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


迴圈語句


1. loop 語句

文法:

loopplsql_sentence;exit when end_condition_expend loop;

plsql_sentence:迴圈體中的PL/SQL 陳述式。至少被執行一遍。

end_condition_exp:迴圈結束條件運算式,當該運算式為 true 時,則程式會退出迴圈體,否則程式將再次執行。


例:

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

SQL> set serveroutput on;SQL> declaresun_i int:=0;i int:=0;beginloopi:=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 loopplsql_sentence;end loop;


condition_expression: 表示一個條件運算式,但其值為 true 時,程式執行迴圈體。

否則 程式退出迴圈體,程式每次執行迴圈體之前,都判斷該運算式是否為 true。

plsql_sentence:迴圈內的plsql語句。


例:

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

declare sum_i int:=0;i int:=0;beginwhile  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 loopplsql_sentence;end loop;


variable_counter_name:表示一個變數,通常為整數類型,用來作為計數器。

預設情況下 計數器的值會遞增,當在迴圈中使用 reverse 關鍵字時,計數器的值會隨迴圈遞減。


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

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

plsql_sentence:迴圈內的plsql語句。


例:

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

declaresum_i int:= 0;beginfor i in reverse 1..100 loopif mod(i,2)=0 then--判斷是否為偶數 sum_i:=sum_i+i;end if;end loop;dbms_output.put_line(‘前100個自然數中偶數和:‘||sum_i);end;/


本文出自 “學如逆水行舟” 部落格,請務必保留此出處http://2226894115.blog.51cto.com/9973734/1956460

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.