读书人

存储过程如何通过游标返回一个结果集

发布时间: 2014-01-28 21:23:02 作者: rapoo

--创建存储过程
create or replace procedure ppp(p out temp%rowtype)
is
cursor a is select * from temp where tid=1;
begin
open a;
fetch a into p;
close a;
end;

--调用
declare
one temp%rowtype;
begin
call ppp(one);
dbms_output.put_line(one.tid);
dbms_output.put_line(one.tname);
end;

(1)我想通过这个存储过程返回一行数据,可老是有报错,以上语句错哪儿了?如果想得到返回的一行数据应该怎么做?
(2)如何通过游标返回一个结果集
需要具体的代码。

------解决方法--------------------------------------------------------
call ppp(one);

去掉call

结果集用REFCURSOR返回就可以了。
------解决方法--------------------------------------------------------

set serveroutput on;
--调用
declare
one temp%rowtype;
begin
ppp(one);
dbms_output.put_line(one.tid);
dbms_output.put_line(one.tname);
end;

------解决方法--------------------------------------------------------
(1)我想通过这个存储过程返回一行数据,可老是有报错,以上语句错哪儿了?如果想得到返回的一行数据应该怎么做?
解:call ppp(one); 调用的时候去掉call 执行使用ppp(one)

(2)如何通过游标返回一个结果集?
在sqlplus中建立如下的内容:
1、程序包

SQL> create or replace package types
2 as
3 type cursorType is ref cursor;
4 end;
5 /

程序包已创建。

2、函数
SQL> create or replace function sp_ListEmp return types.cursortype
2 as
3 l_cursor types.cursorType;
4 begin
5 open l_cursor for select id, title from cf_news order by id;--表的名字
6 return l_cursor;
7 end;
8 /

函数已创建。

3、过程

SQL> create or replace procedure getemps( p_cursor in out types.cursorType )
2 as
3 begin
4 open p_cursor for select id, title from cf_news order by id;--表的名字
5 end;
6 /

过程已创建。

        

读书人网 >oracle

热点推荐