************************************************************************ ****原文:blog.csdn.net/clark_xu 徐長亮的專欄 ************************************************************************
PLSQL是ORACLE在標准SQL基礎上添加了過程化處理,把DML和SELECT語句組織在PLSQL代碼的過程性單元中。
PLSQL擴展了變量和類型。控制語句。過程和函數。
1.1 PLSQL程序結構
PLSQL塊包含:
聲明部分:DECLARE
運行部分:begin
異常處理:exception
比如:
declare
v_AccountID number(5) :=1001;
v_RealName varchar(20)
Begin
select real_name into v_RealName
from account where id=v-Accountid
exception
when no_data_found then
insert into Fee_log(descr)
values(“account 1001 does not exist!”);
commit;
end;
1.2 PLSQL執行過程
client應用程序PLSQL:-----PLSQL引擎(過程性語句運行器)----sql語句運行器
凝視:
單行凝視:——
多行凝視:/* */
1.3變量和數據類型
標量類型包含:數字型。字符型,日期型,布爾型
復雜類型包含:record,associative array,nested table, varray
1.3.1 標量類型
數字類型:
u number
u number的子類型dec(38),float(38),real(38)
u binary_integer(僅僅能用於plsql)
字符型:
u varchar2,varchar
u string(僅僅能用於plsql)
u char
u long
Date
Boolean
u 用於存儲邏輯值
u 不能像數據庫中插入boolean類型的數據
u 不能講列植保存到boolean變量中。
u 僅僅能對boolean變量運行邏輯操作;
1.3.2變量聲明
語法:
var_name type [constant] [not null] [:=value];
PLSQL規定沒有初始化的變量為NULL
比如:
declare
v_realName varchar2(20);
v_baseCost number(7,2) :=5.9;
v_count binary_integer :=0;
1.3.3 %TYPE方式
變量具有和數據庫的表中某列或其它變量同樣的類型。
declare v_realName varchar2(20);
declare v_realName account.real_name%TYPE;
declare
v_tempvar number(7,3) not null :=12.3;
v_anothervar v_tempvar%type :=12.3;
1.4 條件語句
if Boolean_expression1 Then
ELSIF Boolean_expression2 then
ELSE
END IF;
1.5 Loop循環語句
loop
statement1;
statement2;
exit when condition
end loop;
1.6 while循環語句
while Boolean_expresstion loop
statement1;
statement2;
end loop;
1.7 for循環語句
for 循環器 in下限。。上限 loop
statement1;
statement2;
exit when condition
end loop;
1.8 PLSQL中的DML
能夠在PLSQL中石油標准SQL:
1. DML
2. 事務控制語句TCL
比如,begin
insert into host(id) values(’10.0.2.1’);
commit;
end;
PLSQL中本地動態SQL是使用execute immediate語句來運行ddl語句
比如,begin
execute immediate ‘CREATE TABLE TEST(c1 number)’;
end;
DML語句使用本地動態sql來實現的(使用execute immediate語句實現)
比如。begin
execute immediate ‘insert into test values (1)’;
end;
sql語句的編譯發生在PLSQL語句塊的編譯階段;
sql語句的運行發生在PLSQL語句的運行階段;
全部一般這樣寫:
begin
execute immediate ‘CREATE TABLE TEST(c1 number)’;
execute immediate ‘insert into test values (1)’;
end;
此時PLSQL編譯的時候,create語句insert語句為字符串,不須要sql語句編譯;
因此。PLSQL語句完畢建表和插入數據的時候。必須使用execute immediate;
1.9 PLSQL語句中的SELECT語句
依據select語句返回的記錄數。實現分為兩類:
當僅返回一條記錄的時候:
select … into …語句實現;
當返回0條或者多條記錄的時候
用cursor指針來實現
若結果是單行單列,into字句后用標量類型。與字段類型同樣;
若查詢結果為單行多列,into子句后的變量個數,順序,數據類型和select語句后面的目標匹配,也能夠用記錄record類型類記錄;
1.9.1 record類型
語法定義:
type t_cout_rec is record //記錄類型為t_cost_rec
( base_cost cost.base_cost%type,
base_duration cost.base_duration%type;
unit_cost cost.unit_cost%type
)
v_cost t_cost_rec;
v_cost_1 t_cost_rec;
1.9.2 %ROWTYPE
用表結構和視圖結果定義變量。record類型。record成員的名稱和類型與表或者視圖的列名稱和類型全然同樣。
比如,v_cost cost%ROWTYPE;
1.9.3 record變量的引用
begin
v_cost.base_cost:=5.9
v_cost.base_duration:=20;
v_cost.unit_cost:=0.4
v_cost_1=:=v_cost;
select base_cost,base_duration,unit_cost int v_cost_1 from cost;
end;
在inert語句和update語句中使用記錄類型變量
比如,如果cost_t1表事先存在
begin
insert into cost_t1 values v_cost;
update cost_t1 set row=v_cost_1;
commit;
end;
1.10 cursor游標
依據select語句返回的記錄數,若返回記錄數為0條或者多條用cursor實現;
oracle所知曉的每個sql語句都有唯一的cursor與之想相應。
oracle使用專用的sql工作去來運行sql語句。存儲處理信息,這個工作去稱為“cursor”
cursor分為隱式cursor和顯式的cursor
隱式cursor:
--select .. into語句
--DML語句
1.10.1 顯式cursor的處理:
declare聲明游標:床和命名一個sql工作區;
open打開游標
fetch提取游標
推斷是否empty:結果集中是否有未提取的記錄;
close關閉游標
1.10.2 顯式cursor的屬性:
%isopen, 布爾類型,
%notfound,布爾類型。假設前一個fetch語句沒有返回一行記錄,其值為true;
%found,布爾。假設前一個fetch語句返回記錄,其值為true,與%notfound;
%rowcount,數值,到眼下為止,cursor已提取的總行數。
1.10.3 隱式cursor的屬性
sql%isopen, 布爾類型,DML運行中未true,結束后為false
sql%notfound,布爾類型,與sql%found返回值相反
sql%found, 布爾類型,值為true表DML操作成功
sql%rowcount, 數值類型,表示DML語句成功運行的數據行數。
1.10.4 cursor的使用
聲明
在游標聲明中使用標准的select語句,假設須要依照指定的次序處理行,可在查詢中使用order by字句;能夠在查詢中引用變量,可是必須在cursor語句之前聲明這些變量
cursor c_service_id(p_cost_id number) is
select id from service where cost_id=p_cost_id;
open
通過open cursor來運行select語句並標識結果集。select語句假設沒有返回記錄,不會出現異常語法;
open c_service_id(5);
fetch cursor
檢索當前行,把值賦給變量;變量能夠是record類型或者簡單變量;假設是簡單變量,要求例如以下:包括同樣數量的變量;把每個變量和對應的列進行位置匹配。通過循環檢測cursor中是否包括數據行;
fetch cursor_name into [var1,var2,…| recore_name];
fetch c_service_id into v_service_id;
1.10.5 結果集提前的控制
使用循環處理顯式cursor結果集的多行數據;每次fetch一行。重復進行;使用%notfound屬性檢測一次不成功的提前操作;使用顯式cursor的屬性檢測每一次的提前是否成功。避免出現無限循環。
1.10.6 關閉游標
處理完結果集中的是數據后,應該關閉cursor;假設須要,能夠再次打開該curosr。
cursor一旦關閉。全部和該cursor相關的資源都會被釋放。不能夠再從關閉的cursor中提前數據,能夠引起invalid_cursor錯誤;
close c_service_id;