天天看点

oracle和dm7使用sys_refcursor和ref cursor返回结果集

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; ----打印出结果