oracle和dm7使用sys_refcursor和ref cursor傳回結果集
1.二者的差別
ref cursor和sys_refcursor 前者需要單獨聲明,後者直接引用
舉例:
declare
cur_test sys_refcursor;
…
declare
type df_ref is ref cursor;
rf df_ref;
2.如何顯示結果集
1)第一種,直接在存儲過程中,定義了輸出到某個具體列,調用的時候就不用做定義。
CREATE OR REPLACE PROCEDURE “TEST3”
(
PIN IN NUMBER,
POUNT OUT SYS_REFCURSOR)
AS
type ty1 is record(n1 number,n2 number);
t_row ty1;
BEGIN
open pount for select pin,pin+10 from dual;
loop
fetch pount into t_row;
exit when pount%notfound;
PRINT t_row.n1||’ '||t_row.n2; ----達夢用print輸出,oracle用dbms_output.put_line輸出
end loop;
END;
調用:
DECLARE
REFC SYS_REFCURSOR;
BEGIN
TEST3(2,REFC);
end;
顯示結果:2,12(此方法在dm7和oracle都适用)
2)第二種,在存儲過程中,沒有定義輸出,那麼調用的時候就需要指明具體的輸出列。
CREATE OR REPLACE PROCEDURE “TEST5”
(
PIN IN NUMBER,
POUNT OUT SYS_REFCURSOR)
AS
BEGIN
open pount for select pin,pin+10 from dual;
END;
調用:
DECLARE
REFC SYS_REFCURSOR;
type ty1 is record(n1 number,n2 number); —聲明一種自定義類型
t_row ty1; --聲明一個ty1類型的變量
BEGIN
TEST5(2,REFC);
loop
fetch REFC into t_row; —将遊标值賦給變量
exit when REFC%notfound;
PRINT t_row.n1||’ '||t_row.n2; ----達夢用print輸出,oracle用dbms_output.put_line輸出
end loop;
end;
顯示結果:2,12(此方法在dm7和oracle都适用)
3.什麼情況使用rowtype,什麼情況定義record
declare
cur_test sys_refcursor;
(二選一)a
t_row users%rowtype; —當你的輸出結果正好是一個表的所有列,或者一個視圖的所有列
(二選一)b
type type_1 is record(n1 number,n2 varchar2(30)); —當你的輸出不能使用rowtype時,需要自定義所有輸出列的類型。
t_tow type_1;
4.sqlplus中使用refcursor
sqlplus界面:
SQL>var r refcursor; ----或者variable r refcursor;
SQL>exec p_test(:r);
SQL>print r; ----列印出結果