oracle9i 游标返回结果集

lchy110 2010-05-06 05:21:14
直接上代码
create or replace procedure test_cursor(p_1 in number,out_cursor out SYS_REFCURSOR) 
as
begin
open out_cursor for
select t.* from emp t;
end;

这样是可以返回数据集的 但问题是 数据库是从DB2转到oracle里来 以前的程序都是针对db2的 所以存储过程名 参数都定好了。 比如说以前的在DB2里的存储过程 test_cursor 是只有number这个参数的 但到oracle里必须要加个输出参数(游标)--out_cursor out SYS_REFCURSOR, 那这样的话就的改程序了。 所以请教下 能不能将上面的代码改写成 输出游标 不作为参数 。 当然前提是不要用包。例如:
create procedure test_cursor(p_1 in number) as 
/*declare cur cursor with return for
select Uid,UserId,SendNum from userfee where uid in (select Uid from userdata where ucase(loginid)=ucase(piLoginId) and ucase(UserId)<>ucase(piLoginId)) order by uid;
open cur;*/ 这是DB2的写法
...全文
122 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
QKForex 2010-05-09
  • 打赏
  • 举报
回复
能用到 OPEN cursor FOR select 都可以的
lchy110 2010-05-07
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 ptpa 的回复:]

你数据库都改了
程序里面于数据库交互的部分还不改吗
能不能看看你程序里怎么写的
[/Quote] 呵呵 当然 程序是要改些 至少配置文件要改 但如果访问存储过程的参数一样的话 但改动比较少。

无果没法改的话 那我用下面 WKC168的 思路 写
CREATE PROCEDURE "DEV2"."SYNCUSRFEEINFO_B"
("PIUID" IN NUMBER,
"PILASTDBREMAINED" IN NUMBER,
"PIREMAINED" IN NUMBER,
"PIPREPAYUSED" IN NUMBER,
"PIPOSTPAYUSED" IN NUMBER,
out_cursor out SYS_REFCURSOR
)
AS
BEGIN
OPEN out_cursor FOR
SELECT SENDNUM-piPrePayUsed as sendnum from userfee where uid=piUid;

end;

比如这样 也没用到包 这样可以不?
ptpa 2010-05-07
  • 打赏
  • 举报
回复
你数据库都改了
程序里面于数据库交互的部分还不改吗
能不能看看你程序里怎么写的
lchy110 2010-05-07
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wkc168 的回复:]

SQL code

create or replace procedure test_cursor(p_1 in number,out_cursor out SYS_REFCURSOR)
as
v1 emp.col1%type;
v2 emp.col2%type;
v3 emp.col3%type;

begin
open out_cursor for
select……
[/Quote] 首先还是感谢你的再次回答 呵呵 你这个还是不符合我的要求 因为你这个存储过程带了一个输出游标作为参数 但我的初衷是不参数里不能带游标,因为要考虑到代码不能修改了。 还有楼上用包的方法 那样是不是也要改代码了 在VC 和.net里调用包里的存储过程 估计也要改代码了。。
lchy110 2010-05-07
  • 打赏
  • 举报
回复
来人哈。。。
QKForex 2010-05-06
  • 打赏
  • 举报
回复
使用


open P_RES for select *.....;



可以提高查询速度
QKForex 2010-05-06
  • 打赏
  • 举报
回复
可以使用包 和包体

create or replace package PagePK
is
type t_cursor is ref cursor;
procedure retcur(
P_CODE varchar2,
P_MESSAGE out varchar2,
P_SQLSTATUS out varchar2,
P_RES out t_cursor
);

end PagePK;
/

CREATE OR REPLACE package BODY PagePK
is
procedure retcur
(
P_CODE varchar2,
P_MESSAGE out varchar2,
P_SQLSTATUS out varchar2,
P_RES out t_cursor
)
is

P_SQLSTATUS:='0';
P_MESSAGE:='执行成功';

begin
null;

open P_RES for select *.....;

Exception
WHEN OTHERS
Then
P_SQLSTATUS:='-1';
P_MESSAGE:=' (CCGC.GJDM)系统错误:SQLCODE=' || to_CHAR(SQLCODE)||' ';

end retcur;

end PagePK;
心中的彩虹 2010-05-06
  • 打赏
  • 举报
回复

create or replace procedure test_cursor(p_1 in number,out_cursor out SYS_REFCURSOR)
as
v1 emp.col1%type;
v2 emp.col2%type;
v3 emp.col3%type;

begin
open out_cursor for
select col1,col2,col3 from emp t where col=p_1;
for out_cursor_rec in out_cursor
loop
v1:=out_cursor_rec.col1;
v2:=out_cursor_rec.col2;
v3:=out_cursor_rec.col3;
dbms_output.put_line('col1的结果'||to_char(v1)||' '||'col2的结果'||to_char(v2)||' '||'col3的结果'||to_char(v3));
end loop;
close out_cursor;
end;


kingkingzhu 2010-05-06
  • 打赏
  • 举报
回复
类似于全局变量咯 在全局定义 然后子函数直接调用赋值 然后出了子函数接着使用
oracle貌似还没这么fashion
等高手吧
tangren 2010-05-06
  • 打赏
  • 举报
回复
--使用函数
CREATE OR REPLACE FUNCTION test_cursor(p_1 IN NUMBER) RETURN SYS_REFCURSOR IS
out_cursor SYS_REFCURSOR;
BEGIN
OPEN out_cursor FOR
SELECT t.* FROM emp t;
RETURN out_cursor;
END;

lchy110 2010-05-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wkc168 的回复:]

SQL code


create or replace procedure test_cursor(p_1 in number,out_cursor out SYS_REFCURSOR)
as
v1 emp.col1%type;
v2 emp.col2%type;
v3 emp.col3%type;

begin
open out_cursor for
selec……
[/Quote]
首先还是谢谢 楼上两位的解答。。但 wkc168 兄 可能没理解我的意思 。你写的这个代码 还是将out_cursor out SYS_REFCURSOR 做为一个参数了。还是违反了我的初衷。 我的意思是不能将 游标作为参数
心中的彩虹 2010-05-06
  • 打赏
  • 举报
回复

--忘记释放游标了
create or replace procedure test_cursor(p_1 in number,out_cursor out SYS_REFCURSOR)
as
v1 emp.col1%type;
v2 emp.col2%type;
v3 emp.col3%type;

begin
open out_cursor for
select col1,col2,col3 from emp t where col=p_1;
loop
fetch out_cursor into v1,v2,v3;
exit when out_cursor%notfound;
dbms_output.put_line('col1的结果'||to_char(v1)||' '||'col2的结果'||to_char(v2)||' '||'col3的结果'||to_char(v3))
end loop;
close out_cursor;
end;
心中的彩虹 2010-05-06
  • 打赏
  • 举报
回复


create or replace procedure test_cursor(p_1 in number,out_cursor out SYS_REFCURSOR)
as
v1 emp.col1%type;
v2 emp.col2%type;
v3 emp.col3%type;

begin
open out_cursor for
select col1,col2,col3 from emp t where col=p_1;
loop
fetch out_cursor into v1,v2,v3;
exit when out_cursor%notfound;
dbms_output.put_line('col1的结果'||to_char(v1)||' '||'col2的结果'||to_char(v2)||' '||'col3的结果'||to_char(v3))
end loop;
end;


kingkingzhu 2010-05-06
  • 打赏
  • 举报
回复
呵呵 貌似不行 帮顶
可否把外层代码包进来算啦
lchy110 2010-05-06
  • 打赏
  • 举报
回复
眼看就要沉底了。。。顶上去哈

17,090

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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