PL SQL學習筆記 5 流程控制語句

2021-06-02 01:03:38 字數 4194 閱讀 7682

語法寫道

if condition then

statements;

[elsif condition then

statements;]

[else

statements;]

end if; 示例

declare

v_age number(3);

begin

select age into v_age from user

where lower(name) = lower('&&name');

if v_age < 20 then

update user set age = v_age*2 where lower(name) = lower('&&name');

elsif v_age < 40 then

update user set age = v_age*3 where lower(name) = lower('&&name');

else

update user set age = v_age/2 where lower(name) = lower('&&name');

end if;

end;

語法寫道

[單一條件]

case selector

when expression1 then sequence_of_statements1;

when expression1 then sequence_of_statements2;

...when expression1 then sequence_of_statementsn;

[else sequence_of_statementsn+1;]

end case;

[多條件]

case 

when selector_condition1 then sequence_of_statements1;

when selector_condition2 then sequence_of_statements2;

...when selector_conditionn then sequence_of_statementsn;

[else sequence_of_statementsn+1;]

end case; 示例

declare

v_uid user.uid%type;

begin

v_uid := &id;

case v_uid

when 1 then update user set age = 20 where uid = v_uid;

when 2 then update user set age = 40 where uid = v_uid;

else

dbms_out.put_line('不存在該使用者');

end case;

end;

declare

v_uid user.uid%type;

begin

v_uid := &id;

case

when v_uid == 1 then update user set age = 20 where uid = v_uid;

when v_uid == 2 then update user set age = 40 where uid = v_uid;

else

dbms_out.put_line('不存在該使用者');

end case;

end;

語法寫道

loop

statement1;

exit [when condition];

end loop;

使用該語句statement1至少會被執行一次。相當於do...while

示例

delcare

i int :=1;

begin

loop

update user set createdate = sysdate where uid = i;

exit when i = 10 ;

i := i+1;

end loop;

commit;

end;

語法寫道

while condition loop

statement1;

statement2;

...end loop; 示例

declare 

i int := 1;

begin

while i <= 10 loop

update user set createdate = sysdate where uid = i;

i := i+1;

end loop;

commit;

end;

語法寫道

for counter in [reverse]

min_bound..upper_bound loop

statement1;

statement1;

...end loop;

counter : 迴圈控制變數,由oracle隱含定義,不需要顯示定義

min_bound、upper_bound :迴圈控制變數的上下界;

預設情況下for迴圈在每執行一次後,控制變數會自增一;如果指定reverse選項,則會減一

示例

begin

for i in 1..10 loop

update user set createdate = sysdate where uid = i;

end loop;

commit;

end;

-- 該示例中的<>、<>為標號,該名稱可以自定義。

declare

result int;

begin

<>

for i in 1..10 loop

<>

for j in 1..10 loop

result := i * j;

dbms_output.put_line(result);

exit waibu when result = 10; -- 當 result=10 時,退出外部迴圈,本例中是當i=5時退出。相當於呼叫break語句

exit when j = 2; -- 當 j = 2 時,退出內部迴圈。相當於呼叫break語句

end loop neibu;

end loop waibu;

dbms_output.put_line(result);

end;

語法寫道

goto label_name;

goto語句用於跳轉到特定標號處。一般不建議使用

示例

declare

i int := 1;

begin

loop

if i = 10 then

goto jump_loop;

end if;

exit when i > 11;

dbms_output.put_line('i-->'||i); -- 這條語句是不會被執行的!

i := i+1;

end loop;

<>

dbms_output.put_line('i == 10 ! ');

end;

null 語句不執行任何操作,並且直接將控制傳遞到下一條語句。使用null可以提高pl/sql程式的可讀性

declare

v_uid user.uid%type := &di;

v_age user.age%type;

begin

select age into v_age from user where uid = v_uid;

if v_age < 20 then

update user set age = 100 where uid = v_uid;

commit;

else

null;

end if;

end;

Ansible playbook 流程控制語句

1.when條件判斷 root test2 playbook cat test.yml hosts all remote user root tasks name shut down the db server service name mysqld state stopped when ansib...

PLSQL程式設計 流程控制

1.條件分支 語法 created on 2018 8 23 by mengmeng.chen declare local variables here i integer begin test statements here if 條件1 then 執行1 elsif 條件2 then 執行2 e...

PL SQL流程控制語法

set serveroutput on ed 開啟text editer 並輸出結果 dbms output.put line helloword 輸出語句 執行輸出。v sal varchar2 10 0 賦值語句 例子declare v sal sc.score type 獲取相同資料型別 v ...