oracle存储过程如何输出结果集?

applelvv 2010-11-24 09:21:14
网上搜到说用游标实现,我写了如下存储过程,请大侠看看我怎么得不到结果呢?
初学者,不知存储过程写的是否正确,请大家说的详细点。

CREATE OR REPLACE PACKAGE P_SEARCH_ERROR_PACKAGE AS
TYPE OUT_CUR IS REF CURSOR;
end P_SEARCH_ERROR_PACKAGE;


create or replace procedure P_SEARCH_ERROR(v_CO_NBR in varchar2,
v_outmsg out P_SEARCH_ERROR_PACKAGE.OUT_CUR)
AS
begin
declare
v_cfs_co_nbr varchar2(20);
v_cfs_co_id varchar2(16);
oe_hand_flag char(1);
oe_err_info varchar2(2000);
oe_order_error_id number;
begin
open v_outmsg for select distinct cfs.co_nbr,
cfs.co_id,
oe.hand_flag,
oe.err_info,
oe.order_error_id
into v_cfs_co_nbr,v_cfs_co_id,oe_hand_flag,oe_err_info,oe_order_error_id
from sp.crm_for_sa cfs, sp.order_error oe
where cfs.co_id = oe.co_id
and cfs.co_id in (select co_id
from crm.cust_order co
where co.co_nbr = v_CO_NBR)
and oe.hand_flag = 'D';

open v_outmsg for select distinct cfs.co_nbr,
cfs.co_id,
oe.hand_flag,
oe.err_info,
oe.order_error_id
into v_cfs_co_nbr,v_cfs_co_id,oe_hand_flag,oe_err_info,oe_order_error_id
from sp.crm_for_sa cfs, sp.order_error oe
where cfs.order_id = oe.order_id
and cfs.co_nbr = v_CO_NBR
and oe.hand_flag = 'D';

open v_outmsg for select distinct co.co_nbr,
co.co_id,
oe.hand_flag,
oe.err_info,
oe.order_error_id
into v_cfs_co_nbr,v_cfs_co_id,oe_hand_flag,oe_err_info,oe_order_error_id
from crm.cust_order co, sp.order_error oe
where co.co_id = oe.co_id
and co.co_nbr =v_CO_NBR
and oe.hand_flag = 'D';

exception
WHEN OTHERS THEN
ROLLBACK;
end;
end;


...全文
593 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
YY_MM_DD 2010-11-24
  • 打赏
  • 举报
回复

--------你定义的类型也不用了,直接使用游标就行了
------- 变量也不要了
-------into语句有错
create or replace procedure P_SEARCH_ERROR(v_CO_NBR in varchar2,
v_outmsg out sys_refcuror)
AS
v_cfs_co_nbr varchar2(20);
v_cfs_co_id varchar2(16);
oe_hand_flag char(1);
oe_err_info varchar2(2000);
oe_order_error_id number
begin
open v_outmsg for select distinct cfs.co_nbr,
cfs.co_id,
oe.hand_flag,
oe.err_info,
oe.order_error_id
-------into v_cfs_co_nbr,v_cfs_co_id,oe_hand_flag,oe_err_info,oe_order_error_id
from sp.crm_for_sa cfs, sp.order_error oe
where cfs.co_id = oe.co_id
and cfs.co_id in (select co_id
from crm.cust_order co
where co.co_nbr = v_CO_NBR)
and oe.hand_flag = 'D'
union all
select distinct cfs.co_nbr,
cfs.co_id,
oe.hand_flag,
oe.err_info,
oe.order_error_id
----------into v_cfs_co_nbr,v_cfs_co_id,oe_hand_flag,oe_err_info,oe_order_error_id
from sp.crm_for_sa cfs, sp.order_error oe
where cfs.order_id = oe.order_id
and cfs.co_nbr = v_CO_NBR
and oe.hand_flag = 'D'
union all
select distinct co.co_nbr,
co.co_id,
oe.hand_flag,
oe.err_info,
oe.order_error_id
--------into v_cfs_co_nbr,v_cfs_co_id,oe_hand_flag,oe_err_info,oe_order_error_id
from crm.cust_order co, sp.order_error oe
where co.co_id = oe.co_id
and co.co_nbr =v_CO_NBR
and oe.hand_flag = 'D';

loop
fetch v_outmsg into v_cfs_co_nbr,v_cfs_co_id,oe_hand_flag,oe_err_info,oe_order_error_id;
exit when v_outmsg notfound;
dbms_output.put_line('v_cfs_co_nbr:'||v_cfs_co_nbr) ;
dbms_output.put_line('v_cfs_co_id:'||v_cfs_co_id) ;
dbms_output.put_line('oe_hand_flag:'||oe_hand_flag) ;
dbms_output.put_line('oe_err_info:'||oe_err_info) ;
dbms_output.put_line('oe_order_error_id:'||oe_order_error_id) ;
end loop
close v_outmsg;
exception
WHEN OTHERS THEN

end;
applelvv 2010-11-24
  • 打赏
  • 举报
回复
控制台输出,请问怎么搞呢?多谢
YY_MM_DD 2010-11-24
  • 打赏
  • 举报
回复
还有错。。自己调试下,上面修改的多了第4行的begin
YY_MM_DD 2010-11-24
  • 打赏
  • 举报
回复

--------你定义的类型也不用了,直接使用游标就行了
------- 变量也不要了
-------into语句有错
-------应用程序操作游标
create or replace procedure P_SEARCH_ERROR(v_CO_NBR in varchar2,
v_outmsg out sys_refcuror)
AS
begin
declare
begin
open v_outmsg for select distinct cfs.co_nbr,
cfs.co_id,
oe.hand_flag,
oe.err_info,
oe.order_error_id
-------into v_cfs_co_nbr,v_cfs_co_id,oe_hand_flag,oe_err_info,oe_order_error_id
from sp.crm_for_sa cfs, sp.order_error oe
where cfs.co_id = oe.co_id
and cfs.co_id in (select co_id
from crm.cust_order co
where co.co_nbr = v_CO_NBR)
and oe.hand_flag = 'D'
union all
select distinct cfs.co_nbr,
cfs.co_id,
oe.hand_flag,
oe.err_info,
oe.order_error_id
----------into v_cfs_co_nbr,v_cfs_co_id,oe_hand_flag,oe_err_info,oe_order_error_id
from sp.crm_for_sa cfs, sp.order_error oe
where cfs.order_id = oe.order_id
and cfs.co_nbr = v_CO_NBR
and oe.hand_flag = 'D'
union all
select distinct co.co_nbr,
co.co_id,
oe.hand_flag,
oe.err_info,
oe.order_error_id
--------into v_cfs_co_nbr,v_cfs_co_id,oe_hand_flag,oe_err_info,oe_order_error_id
from crm.cust_order co, sp.order_error oe
where co.co_id = oe.co_id
and co.co_nbr =v_CO_NBR
and oe.hand_flag = 'D';

exception
WHEN OTHERS THEN
ROLLBACK;
end;
end;
YY_MM_DD 2010-11-24
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 applelvv 的回复:]
我的需求很简单,我再描述下:

有多张表,比如Table1,Table2,Table3,我想写个存储过程来完成以下功能:

(1)从Table1和Table2中通过Select语句来选择得到record1(通过某种条件来选择);
(2)从Table2和Table3中通过Select语句选择得到record2;
(3)record1和record2中可能有多条结果,也可能为空,我想把这些……
[/Quote]
输出到控制台,还是供应用程序使用呢?
applelvv 2010-11-24
  • 打赏
  • 举报
回复
我的需求很简单,我再描述下:

有多张表,比如Table1,Table2,Table3,我想写个存储过程来完成以下功能:

(1)从Table1和Table2中通过Select语句来选择得到record1(通过某种条件来选择);
(2)从Table2和Table3中通过Select语句选择得到record2;
(3)record1和record2中可能有多条结果,也可能为空,我想把这些结果全部输出;
huangyunzeng2008 2010-11-24
  • 打赏
  • 举报
回复
需求不明确,不开工!
applelvv 2010-11-24
  • 打赏
  • 举报
回复
还有这些变量为什么没有用,

凭你的经验,你觉得我的程序里面还有哪些问题呢?

谢谢
applelvv 2010-11-24
  • 打赏
  • 举报
回复


请问大侠,这些变量应该往哪里放呢/

xiexie
gelyon 2010-11-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 applelvv 的回复:]
我想通过三个查询显示所有结果,就是三个查询之间是union all的关系。通过存储过程来查询结果集。
[/Quote]
那你就用union all 啊
open v_outmsg for
(
select。。。from ...
union all
select .....
union all
select ....
)

还有你定义的变量没用,而且地方也错了
applelvv 2010-11-24
  • 打赏
  • 举报
回复
我想通过三个查询显示所有结果,就是三个查询之间是union all的关系。通过存储过程来查询结果集。
YY_MM_DD 2010-11-24
  • 打赏
  • 举报
回复
[Quote=引用楼主 applelvv 的回复:]
网上搜到说用游标实现,我写了如下存储过程,请大侠看看我怎么得不到结果呢?
初学者,不知存储过程写的是否正确,请大家说的详细点。

CREATE OR REPLACE PACKAGE P_SEARCH_ERROR_PACKAGE AS
TYPE OUT_CUR IS REF CURSOR;
end P_SEARCH_ERROR_PACKAGE;


create or rep……
[/Quote]
1.你的游标被赋值了3次,最后一次肯定要覆盖前两次。
2.你打开游标的into是什么呢?
3.及时into给了几个局部变量,你的局部变量也没起作用呢。
4.你这个存储过程的最终目的是什么?就返回游标结果集,还是要做其他的操作呢?
这个你必须明确啊。。

17,089

社区成员

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

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