高分求解:怎样才能用 select 从游标变量中取得结果集

牛意思 2004-08-25 05:04:36
有两个表: 表A 和 表B ,
同时有两个存储过程:proc_a 和 proc_b
这两个存储过程分别从表A和表B中查询数据,然后以游标变量的方式返回结果集,即:

proc_a(result_set out pkg_cursor.cur_type),
proc_b(result_set out pkg_cursor.cur_type)

现在的问题是,我想创建存储过程C,
proc_c(result_set out pkg_cursor.cur_type)

也是返回结果集,其中想用到proc_a和proc_b的结果

不知道oracle支不支持类似这样的语法:

open result_set for
select *
from a_cur

这里a_cur是从proc_a得到的游标变量

我这个问题也可以等同于:

如果不使用fetch的方法,如何得到游标变量中的记录?






...全文
320 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
qzg 2004-09-02
  • 打赏
  • 举报
回复
我遇到你这种情况的时候都是将结果急做为一个临时的数据插入到一个表当中,然后在前端将自己需要的数据再过滤出来。虽然笨了一点,但也不失为一个解决问题的办法。
liuyi8903 2004-09-01
  • 打赏
  • 举报
回复
返回结果集:

轉自OTN網,beckham發表
返回记录集过程:
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;
/
--------------------------------------------------------------------------------------------------------------
返回记录集函数:
1、建立测试表
CREATE TABLE student
(
id NUMBER,
name VARCHAR2(30),
sex VARCHAR2(10),
address VARCHAR2(100),
postcode VARCHAR2(10),
birthday DATE,
photo LONG RAW
);
/

2、建立带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;
/

3、用pl/sql块进行测试:
declare
w_rc pkg_test.myrctype; --定义ref cursor型变量

--定义临时变量,用于显示结果
w_id student.id%type;
w_name student.name%type;
w_sex student.sex%type;
w_address student.address%type;
w_postcode student.postcode%type;
w_birthday student.birthday%type;

begin
--调用函数,获得记录集
w_rc := pkg_test.get(1);

--fetch结果并显示
loop
fetch w_rc into w_id,w_name,w_sex,w_address,w_postcode,w_birthday;
exit when w_rc%notfound;
dbms_output.put_line(w_name);
end loop;
end;

4、测试结果:
通过。
--------------------------------------------------------------------------------------------------------------
返回对象类型:
SQL> create table a (id number,name varchar2(50),doctime date);

Table created.

--插入六条测试数据:
SQL> insert into a values (1,'aaa',to_date('2002-07-01','yyyy-mm-dd'));

1 row created.

SQL> insert into a values (2,'bbb',to_date('2002-07-02','yyyy-mm-dd'));

1 row created.

SQL> insert into a values (3,'ccc',to_date('2002-07-03','yyyy-mm-dd'));

1 row created.

SQL> insert into a values (4,'ddd',to_date('2002-07-04','yyyy-mm-dd'));

1 row created.

SQL> insert into a values (5,'eee',to_date('2002-07-05','yyyy-mm-dd'));

1 row created.

SQL> insert into a values (6,'fff',to_date('2002-07-06','yyyy-mm-dd'));

1 row created.

SQL> commit;

Commit complete.

--创建两个type
SQL> create or replace type myobjectype as object (x int,y date,z varchar2(50));
2 /

Type created.

SQL> create or replace type mytabletype as table of myobjectype
2 /

Type created.

liuyi8903 2004-09-01
  • 打赏
  • 举报
回复
哦。fetch下面还少一句
exit when v_cur%notfound;
liuyi8903 2004-09-01
  • 打赏
  • 举报
回复
这是存储过程里的使用游标的方法之一
create or replace peocefure p_test(v_id in varchar2,result out varchar2)is
type v_cursor is ref cursor;
v_cur v_cursor;
v_result varchar2(100);
begin
open v_cur for select .....;
loop
fetch v_cur into v_result;
...
...
end loop;
close v_cur;
end p_test;
牛意思 2004-09-01
  • 打赏
  • 举报
回复
只好自己顶一下了,希望大家来关注
牛意思 2004-08-31
  • 打赏
  • 举报
回复
如果仅仅使用一个结果集当然不会有问题,
但是如果是这样:

open result_set for
select *
from a_cur
union all
select *
from b_cur

怎么办?
wylwyl1130 2004-08-29
  • 打赏
  • 举报
回复

你把
open result_set for
select *
from a_cur
改成
result_set := a_cur
试一下

我自己做的没有问题
begin

open c_cursor for select * from student_1;
c_cursor2:=c_cursor;

loop
/*fetch c_cursor into v_sid;
exit when c_cursor%notfound;*/
fetch c_cursor2 into v_sid2;
exit when c_cursor2%notfound;

DBMS_OUTPUT.PUT_LINE(v_sid2.sage);
end loop;
end
/

牛意思 2004-08-27
  • 打赏
  • 举报
回复
没有别的办法了吗?
yujiabian 2004-08-26
  • 打赏
  • 举报
回复
oracle好像不支持用游标中来建游标和open for 类型的游标变量
牛意思 2004-08-26
  • 打赏
  • 举报
回复
我知道索引表的办法,但是如果

proc_a 和 proc_b 返回的结果集是动态的呢?就是说无法事先知道结果集的内容


to zhpsam109(孤寂无边) :

proc_a 和 proc_b 中实现的是复杂的业务逻辑,无法用简单的SQL表达


to niuanxin:

两个问题:
1,你的办法的前提是事先知道游标的内容,
2,如果我在这个存储过程中最终要返回结果集而不是dbms_output怎么办?

还有,游标和游标变量是不同的概念

liuxing23 2004-08-25
  • 打赏
  • 举报
回复
proc_a(result_set out pkg_cursor.cur_type),
proc_b(result_set out pkg_cursor.cur_type)
这两个过程的输出可以用索引表返回。
zhpsam109 2004-08-25
  • 打赏
  • 举报
回复
open result_set for
select a.*,b.*
from a,b where .........
这样不行吗?
niuanxin 2004-08-25
  • 打赏
  • 举报
回复
create or replace procedure my_test as

cursor my_cursor is
select to_char(sysdate, 'yyyy') year, to_char(sysdate, 'mm') month
from dual;

Begin

for p in my_cursor loop
sys.dbms_output.put_line('year ==> ' || p.year);
sys.dbms_output.put_line('month ==> ' || p.month);
end loop;

End my_test;



关注!!!
wylwyl1130 2004-08-25
  • 打赏
  • 举报
回复
for c1 in result_set loop
不需要fetch

oracle好像不支持用游标中来建游标和open for 类型的游标变量

3,490

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧