目前有一个需求,需要根据外部程序传入的参数到数据库中取得一个记录集返回;很多例子都是通过使用package定义一个refcursor,然后open这个游标返回,但是问题是对于简单的查询可以用 open 游标 for select ...返回,现在我的查询比较复杂,不能直接通过一个sql取得,比如,需要根据输入的一个时间范围,每一天为一行,对于每一行中的某些字段,需要根据时间通过一些计算处理,现在有什么办法能实现该功能呢?
[code=SQL]create or replace procedure test_pro(vid tj_test.id%type,curtest out SYS_REFCURSOR) as vdata tj_test%rowtype; begin open curtest for select id,name,age from tj_test where id <= vid; loop fetch curtest into vdata; exit when curtest%notfound; dbms_output.put_line(vdata.id); end loop; close curtest; end test_pro
create or replace procedure test_pro(vid tj_test.id%type,curtest out SYS_REFCURSOR) as vdata tj_test%rowtype; begin open curtest for select id,name,age from tj_test where id <= vid; loop fetch curtest into vdata; exit when curtest%notfound; dbms_output.put_line(vdata.id); end loop; close curtest; end test_pro
这样没有问题,但是我的问题是,游标不能直接简单的通过select id,name,age from tj_test where id <= vid这样select语句取得,而是需要对取得记录的每一行重新进行处理,然后赋值,作为一个新的记录集返回。
参考这个帖子
vdata tj_test%rowtype;
begin
open curtest for select id,name,age from tj_test where id <= vid;
loop
fetch curtest into vdata;
exit when curtest%notfound;
dbms_output.put_line(vdata.id);
end loop;
close curtest;
end test_pro
vdata tj_test%rowtype;
begin
open curtest for select id,name,age from tj_test where id <= vid;
loop
fetch curtest into vdata;
exit when curtest%notfound;
dbms_output.put_line(vdata.id);
end loop;
close curtest;
end test_pro
二.定义一个table类型,把每行处理结果插入进去,然后返回table类型变量.
三.建立一个临时表,把处理结果插入临时表,然后程序对表进行读取就可以了