Oracle入门(十四A)之PL/SQL 基本结构
生活随笔
收集整理的這篇文章主要介紹了
Oracle入门(十四A)之PL/SQL 基本结构
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、條件控制語句?
(1)條件語句I
if…then…end if形式1:
if <布爾表達式> then…(pl/sql和sql)…end if;(2)條件語句II
?if…then…else … end if形式2:
if <布爾表達式> then…(pl/sql和sql)else…end if;
(3)條件控制語句III
?if…then…elsif…then… else … end if形式3:
if <布爾表達式1> then…(pl/sql和sql)elsif <布爾表達式2> then…else…end if;
例子:
declaretheGrade number:= 88; beginif theGrade>=90 thendbms_output.put_line('杰出');elsif theGrade>=80 thendbms_output.put_line('優秀');elsif theGrade>=60 thendbms_output.put_line('合格');elsedbms_output.put_line('不及格');end if; end;二、循環
循環語句分類
(1)簡單循環
Loop…(PL/SQl和SQL)End loop;可通過以下語句強行跳出循環:
1) if <布爾表達式> then exit;
2) exit when <布爾表達式>;
例子:
set serveroutput on; declarei number(8):=5; begin<<first_loop>>loopdbms_output.put_line('i = '||i);i:= i-1;exit first_loop when i = 0;end loop;dbms_output.put_line('LOOP循環已經結束!'); end;(2)For循環
For n in num1..num2 Loop… (PL/SQl和SQL) End loop;共循環 num2-num1+1 次;
例子:
beginFor循環for i in -3..3 loopdbms_output.put_line('i = '||i);end loop;dbms_output.put_line('FOR循環已經結束!'); end;(3)While循環
控制條件為真時,重復執行循環體內的語句。
例子:
declarei number(8):=5; While循環 beginwhile(i > 0) loopdbms_output.put_line('i = '||i);i:=i-1;end loop while_loop;dbms_output.put_line('WHILE循環已經結束!'); end;(4)Case
A.Case語句I
casewhen <表達式1> then…when <表達式2> then…else…end case;B.Case語句II?
?格式2:將列值轉換成說明
Case 列名when 值1 then 說明1when 值2 then 說明2…else 其他 End;例子:
set serveroutput Case語句on declaregender varchar2(20):= '男'; begincase genderwhen '男' then dbms_output.put_line('勇敢');when '女' then dbms_output.put_line('漂亮');else dbms_output.put_line('人妖');end case; end;(5)Goto語句?
goto 標簽名語句… <<標簽名>>跳轉規則:
? 1)同一程序塊內跳轉
? 2)子塊跳到父塊,不能父塊跳到子塊
? 3)不能從IF語句外跳入IF語句內
? 4)不能從While語句外跳入While語句內
? 5)不能從子程序外跳入子程序內
例子:
DECLAREi number; BEGINi:=5;<<repeat_loop>> --循環點DBMS_OUTPUT.PUT_LINE('i='||i);i:=i-1;IF i>0 THENGOTO repeat_loop; --小于5,就goto到repeat_loopEND IF; END;總結
以上是生活随笔為你收集整理的Oracle入门(十四A)之PL/SQL 基本结构的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 挡拆是什么意思 什么是挡拆
- 下一篇: 匡正是什么意思 匡正的意思