共計(jì) 3826 個(gè)字符,預(yù)計(jì)需要花費(fèi) 10 分鐘才能閱讀完成。
本篇文章給大家分享的是有關(guān)怎么進(jìn)行 PLSQL 重點(diǎn)問(wèn)題理解和實(shí)戰(zhàn),丸趣 TV 小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著丸趣 TV 小編一起來(lái)看看吧。
一 ORACLE 中 PL/SQL 使用的集合變量類型有 RECORD(類)、VARRAY(sql 數(shù)組)、TABLE(嵌套表)
TABLE(嵌套表) 可以加 index 定義也可以不加,加表示 index by 是建立主鍵索引相當(dāng)于數(shù)組,不加就是個(gè)嵌套表集合
1 TABLE(嵌套表) 定義表變量類型
type type_table_emp_empno is table of emp.empno%type index by binary_integer;–TYPE 表示表中一行中字段類型
v_empnos type_table_emp_empno;
如果用 %type 定義
定義集合變量 v_empnos 是一個(gè)有 emp.empno 字段類型的數(shù)組,自己理解是存放實(shí)際還是一個(gè)表,里面只有一個(gè)字段,且字段上有索引
對(duì)此集合變量(is table of index by) 類型的操作 包括 count,delete,但不能用 trim
對(duì) VARRAY 可以用 count,delete 和 trim
使用形式
select to_char(truck_in_out_id),
employee_id,
employee_nm,
truck_in_purpose
bulk collect into
carid,
empid,
empnm,
dest
forall i in 1 .. carid.COUNT
update cpnew.CP_VISIT_APPLY a
set a.mgr_principal_id = empid(i),
a.mgr_principal_nm = empnm(i),
a.visit_dest = dest(i)
where a.visit_apply_id = carid(i)
and a.mgr_principal_id is null;
type delArray1 is table of TEST.COURSE%ROWTYPE index by binary_integer; –ROWTYPE 表示表中一行的記錄類型
cur_result delArray1;
如果用 %rowtype 定義
定義集合變量 cur_result 是一個(gè) COURSE 表類型的集合,自己理解是按一個(gè)表存放,里面包括 COURSE 的所有字段類型,且用整形數(shù)做這個(gè)表的索引
使用形式
select * bulk collect into cur_result
forall i in 1 .. cur_result.COUNT
update cpnew.CP_VISIT_APPLY a
set a.mgr_principal_id = cur_result(i).empid,
a.mgr_principal_nm = cur_result(i).empnm,
a.visit_dest = cur_result(i).dest
where a.visit_apply_id = cur_result(i).carid
and a.mgr_principal_id is null;
實(shí)際工作中的例子
plsql 大數(shù)據(jù)量刪除,修改的方法 FORALL 加 bulk collection into
create or replace procedure zl_del_UPDATEAPPLY_DEST187 as
–type ridArray is table of rowid index by binary_integer;
type delArray1 is table of varchar2(32) index by binary_integer;
type delArray2 is table of CP_2012.CP_VISIT_TRUCK_INOUT.employee_id%type index by binary_integer;
type delArray3 is table of CP_2012.CP_VISIT_TRUCK_INOUT.employee_nm%type index by binary_integer;
type delArray4 is table of CP_2012.CP_VISIT_TRUCK_INOUT.truck_in_purpose%type index by binary_integer;
// 你會(huì)發(fā)現(xiàn)用 %type 就得每個(gè)字段都得定義他的類型
carid delArray1;
empid delArray2;
empnm delArray3;
dest delArray4;
begin
select to_char(truck_in_out_id),
employee_id,
employee_nm,
truck_in_purpose
bulk collect into
carid,
empid,
empnm,
dest
from CP_2012.CP_VISIT_TRUCK_INOUT;
–where rownum 600001;
forall i in 1 .. carid.COUNT
update cpnew.CP_VISIT_APPLY a
set a.mgr_principal_id = empid(i),
a.mgr_principal_nm = empnm(i),
a.visit_dest = dest(i)
where a.visit_apply_id = carid(i)
and a.mgr_principal_id is null;
DBMS_OUTPUT.PUT_LINE(to_char(carid.COUNT) ||
records deleted from temp_mid_hubei_bak !!!
end;
這種方法最大缺點(diǎn)是 forall 里不能訪問(wèn)遠(yuǎn)程表,也不能用 dblink,且只能放 dml 語(yǔ)句不能用 dbms.putline
經(jīng)過(guò)測(cè)試過(guò)發(fā)現(xiàn) for 可以替代 forall 盡管時(shí)間相對(duì)慢一點(diǎn),但也能接受,所以可以在 for 中用 dblink,相應(yīng)語(yǔ)句如下:
for i in 1 .. carid.COUNT
loop
update cpnew.CP_VISIT_APPLY@LINK_213TO187_CPNEW a
set a.mgr_principal_id = empid(i),
a.mgr_principal_nm = empnm(i),
a.visit_dest = dest(i)
where a.visit_apply_id = carid(i)
and a.mgr_principal_id is null;
2 Record 變量類型:(相當(dāng)于 java 的類)
定義
type type_record_dept is record
(
deptno dept.deptno%type,
dname dept.dname%type,
loc dept.loc%type
);
v_temp type_record_dept;
3 VARRAY
定義和使用
CREATE OR REPLACE TYPE numbers_t IS VARRAY (5) OF NUMBER
DECLARE
l_list numbers_t:= numbers_t (1, 2, 3, 4, 5);
BEGIN
l_list.DELETE;
DBMS_OUTPUT.put_line (CASE l_list.COUNT WHEN 0 THEN Empty END);
END;
數(shù)組使用例子 參考 Oracle 數(shù)組的使用 http://blog.itpub.net/12932950/viewspace-351791/
還可以對(duì)比本人之前的 blog 查看游標(biāo)和 bulk collect into 的用法 http://blog.itpub.net/750077/viewspace-2075986/
二 PL/SQL 異常
異常類型 1 預(yù)定義的異常處理,2 非預(yù)定義 (Predefined)錯(cuò)誤,3 用戶定義 (User_define) 錯(cuò)誤
一般預(yù)定義和用戶定義異常使用較多
1 預(yù)定義異常 如 oracle 已定義的異常
ORA-1403 No_data_found SELECT INTO 沒(méi)有找到數(shù)據(jù)
使用時(shí)如果 select 沒(méi)查出數(shù)據(jù)時(shí)就直接處理沒(méi)找到數(shù)據(jù)的異常
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE(數(shù)據(jù)庫(kù)中沒(méi)有編碼為 ||v_empno|| 的員工
END;
2 用戶自定義異常
用戶先定義異常
no_result EXCEPTION;
如沒(méi)有更新的數(shù)據(jù)時(shí),拋出這個(gè)異常
UPDATE employees SET salary = salary+100 WHERE employee_id = v_empno;
IF SQL%NOTFOUND THEN
RAISE no_result;
END IF;
然后處理這個(gè)異常
EXCEPTION
WHEN no_result THEN
DBMS_OUTPUT.PUT_LINE(你的數(shù)據(jù)更新語(yǔ)句失敗了!
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLCODE|| — ||SQLERRM);
END;
SQLCODE,SQLERRM 是 ORACLE 函數(shù),會(huì)打印錯(cuò)誤代碼和錯(cuò)誤名稱
以上就是怎么進(jìn)行 PLSQL 重點(diǎn)問(wèn)題理解和實(shí)戰(zhàn),丸趣 TV 小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注丸趣 TV 行業(yè)資訊頻道。