儲存過程返回結果集。*過程返回記錄集。
CREATE OR REPLACE PACKAGE pkg_test
AS
TYPE myrctype IS REF CURSOR;
PROCEDURE get (p_id NUMBER, p_rc OUT myrctype);
END pkg_test;
/
CREATE OR REPLACE PACKAGE BODY pkg_test
AS
PROCEDURE get (p_id NUMBER, p_rc OUT myrctype)
IS
sqlstr VARCHAR2 (500);
BEGIN
IF p_id = 0 THEN
OPEN p_rc FOR
SELECT ID, NAME, sex, address, postcode, birthday
FROM student;
ELSE
sqlstr :=
'select id,name,sex,address,postcode,birthday
from student where id=:w_id';
OPEN p_rc FOR sqlstr USING p_id;
END IF;
END get;
END pkg_test;
/
函式返回記錄集:
建立帶ref cursor定義的包和包體及函式:
CREATE OR REPLACE
package pkg_test as
/* 定義ref cursor型別
不加return型別,為弱型別,允許動態sql查詢,
否則為強型別,無法使用動態sql查詢;
*/
type myrctype is ref cursor;
--函式申明
function get(intID number) return myrctype;
end pkg_test;
/
CREATE OR REPLACE
package body pkg_test as
--函式體
function get(intID number) return myrctype is
rc myrctype; --定義ref cursor變數
sqlstr varchar2(500);
begin
if intID=0 then
--靜態測試,直接用select語句直接返回結果
open rc for select id,name,sex,address,postcode,birthday from
student;
else
--動態sql賦值,用:w_id來申明該變數從外部獲得
sqlstr := 'select id,name,sex,address,postcode,birthday from
student where id=:w_id';
--動態測試,用sqlstr字串返回結果,用using關鍵詞傳遞引數
open rc for sqlstr using intid;
end if;
return rc;
end get;
end pkg_test;