公司需求知識自學
簡化應用設計
你不覺得把存儲過程分門別類是很重要的麼
用package不僅能把存儲過程分門別類
copy and pasty
也有方便的第三方工具了
用第三方工具吧
是包也是一種命名pl/sql塊
包的作用:包可以將任何出現在塊聲明的語句(過程
包分為兩部分:包頭和包體
如何創建包?
語法格式:
CREATE OR REPLACE PACKAGEpackage_name /*包頭名稱*/
IS|AS pl/sql_package_spec /*定義過程
定義包頭應當遵循以下原則:
語法格式:
CREATE OR REPLACE PACKAGE BODY package_name/*包名必須與包頭的包名一致*/
IS | AS pl/sql_package_body /*游標
包體是與包頭相互獨立的
EG:定義一個包頭
CREATE
OR
REPLACE
PACKAGE select_table
IS
TYPE tab_
IS
RECORD
(
itnum_ varchar ( )
itnum_ varchar ( )
);
TYPE tab_
IS
RECORD
(
itnum_ varchar ( )
itnum_ varchar ( )
itnum_ varchar ( )
);
TYPE tab_
IS
RECORD
(
itnum_ varchar ( )
itnum_ varchar ( )
itnum_ varchar ( )
itnum_ varchar ( )
);
TYPE tab_
IS
RECORD
(
itnum_ varchar ( )
itnum_ varchar ( )
itnum_ varchar ( )
itnum_ varchar ( )
itnum_ varchar ( )
);
TYPE tab_
IS
RECORD
(
itnum_ varchar ( )
itnum_ varchar ( )
itnum_ varchar ( )
itnum_ varchar ( )
itnum_ varchar ( )
itnum_ varchar ( )
);
TYPE cur_
IS
REF
CURSOR
RETURNtab_ ;
TYPE cur_
IS
REF
CURSOR
RETURNtab_ ;
TYPE cur_
IS
REF
CURSOR
RETURNtab_ ;
TYPE cur_
IS
REF
CURSOR
RETURNtab_ ;
TYPE cur_
IS
REF
CURSOR
RETURNtab_ ;
END
select_tab;
EG:
CREATE
OR
REPLACE
PACKAGE test_package
IS
FUNCTION
average
(cnum
IN
char
)
RETURN
NUMBER;
PRODURE student_grade
(CUR
OUT
select_table cur_ );
CUR的數據類型是select_table包中cur_o
END
test_package;
包體:
CREATE
OR
REPLACE
PACKAGE BODY test_package
IS
/*函數實現開始*/
FUNCTION
average
(cnum
IN
char
)
RETURN
NUMBER;
AS
avger NUMBER;
BEGIN
SELECT
AVG
(CJ)
INTO
avger
FROM
XS_KC
WHERE
KCH=cnum
GROUP
BYKCH;
RETURN
(avger);
END
average;
/*函數實現結束*/
/*過程實現開始*/
PRODURE student_grade
(CUR
OUT
select_table cur_ );
AS
OPEN
CUR
FOR
SELECT
XS XH XS XM KC KCM XS_KC CJ
FROM
XS XS_KC KC
WHERE
XS XH =XS_KC XH
AND
XS_KC KCH=KC KCH;
END
student_grade;
/*過程實現結束*/
END test_package;
重載:包中的函數和過程可以重載
以下條件不能重載:
PROCEDURE overloadME(p_theparameterIN number);
PROCEDURE overloadME(p_theparameterOUT number);
IN
如:
FUNCTION overloadMeETooRETURN DATE;
FUNCTION overloadMeETooRETURN NUMER;
PROCEDURE overloadME(p_theparameterIN char);
PROCEDURE overloadME(p_theparameterIN varchar
當第一次調用打包子程序時
在大多數情況下
語法格式:
CREATE
OR
REPLACE
PACKAGE BODYpackage_name
IS
|
AS
BEGIN
Initialization_code;
要運行的初始化代碼
END
;
ORACLE 內置包
除了UTL_FILE包存儲在服務器和客戶端外
From:http://tw.wingwit.com/Article/program/Oracle/201311/16818.html