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;
<>
在pl/sql中,可以使用<>的形式定義乙個標籤,使用goto關鍵字即可讓程式在執行時跳轉至該標籤,從而結束迴圈體。 PLSQL語法基礎
變數普通變數宣告方式示例 created on 2019 7 17 by zhou declare v name varchar 20 v sal number v addr varchar 20 begin v name 張三 v sal 10000 select 長沙 into v addr f...
PL SQL之基礎語法
declare 說明部分 變數說明 游標申明 例外說明 begin 語句序列 dml語句 exception 例外處理語句 end 1 定義基本變數 2 型別 char,varchar2,date,number,boolean,long 3 舉例 var1 char 15 married boole...
PL SQL 概述與基礎語法
第乙個pl sql程式 開啟輸出開關 set serveroutput on declare 說明部分 變數,游標或者例外 begin 程式體 dbms output.put line helllo world end dbms output 程式包 檢視程式包的結構 desc dbms outpu...