標籤:基本文法 結構 pl/sql
1.分支結構
PL/SQL中,使用if關鍵字作為分之結構的程式起始段。
總體有以下幾種分支結構:
1)if condition then statement end if;
2)if condition then statement
else then statement
end if;
3)if condition then statement
elsif condition then statement
...
else then statement
end if;
分別對應於其它語言中的if(){},if(){}else(){},if(){}else if(){}...else{}結構
此外,PL/SQL還提供case關鍵字作為分支結構的標識段。
case的用法有以下幾種:
1)對某個欄位進行單值匹配
case column1
when value1 then statement
when value2 then statement
...
end case;
這種結構類似於其它語言中的switch結構。
2)使用case..when結構來表達if..else條件文法
case
when condition then statement
when condition then statement
...
end case;
2.迴圈結構
PL/SQL中,具體的迴圈結構有以下幾種:
1)for
for i in rs.first..rs.last loop
end loop;
這種結構在遍曆遊標指向的集合時,顯得簡潔而高效,該結構可實現自動管理遊標:包括遊標的開啟、關閉和自動fetch。
樣本:
declare
cursor c_emp is
select * from emp;
emp_record emp%rowtype;
begin
for emp_record in c_emp
loop
dbms_output.put_line(emp_record.empno);
end loop;
end;
這段PL/SQL程式並未開啟遊標,也沒有使用fetch語句,更沒有關閉遊標,但是使用for..in結構可以自動隱式完成這些工作,因此簡潔高效,且不易出錯。
2)loop when
loop
exit when (statement)
end loop;
3)loop while
while (statement)
loop
end loop;
4)goto
loop
if (statement) then goto label_name
end if;
end loop;
<<label_name>>
在PL/SQL中,可以使用<<label_name>>的形式定義一個標籤,使用goto關鍵字即可讓程式在執行時跳轉至該標籤,從而結束迴圈體。
本文出自 “notDebug” 部落格,轉載請與作者聯絡!
PL/SQL文法