Oracle中怎么用游标作为输出参数,写点代码举例吧!

 我来答
山水阿锐
推荐于2016-05-12 · TA获得超过34.3万个赞
知道顶级答主
回答量:23.7万
采纳率:91%
帮助的人:3.2亿
展开全部
您好,要自己定义一个type [cur_name] is ref cursor游标,返回的时候就直接 procedure AAA(变量名 out [cur_name])如此申明OUT变量
存储过程 用系统默认的 sys_refcursor 游标类型 定义变量就OK了
1、Sql代码
--PL/SQL Code (包中带过程) 过程带游标的OUT参数,返回游标(ref cursor)
create or replace package my_pack as
type my_ref_cursor is ref cursor;
procedure getMyCursor(val out my_ref_cursor);
end my_pack;

create or replace package body my_pack as
procedure getMyCursor(val out my_ref_cursor)
is
begin
open val for select * from student;
end;
end my_pack;

--PL/SQL Code (包中带过程) 过程带游标的OUT参数,返回游标(ref cursor)
create or replace package my_pack as
type my_ref_cursor is ref cursor;
procedure getMyCursor(val out my_ref_cursor);
end my_pack;

create or replace package body my_pack as
procedure getMyCursor(val out my_ref_cursor)
is
begin
open val for select * from student;
end;
end my_pack;

2、Java代码
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@XX.XX.XX.XXX:XXXX:oracle9i","XXX_temp","XXX_temp");
DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
CallableStatement cs = conn.prepareCall("{ call my_pack.getMyCursor(?) }");
cs.registerOutParameter(1,OracleTypes.CURSOR);
cs.execute();
ResultSet rs = ((OracleCallableStatement)cs).getCursor(1);
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+ rs.getDate(4)+" "+rs.getString(5));
}

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@XX.XX.XX.XXX:XXXX:oracle9i","XXX_temp","XXX_temp");
DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
CallableStatement cs = conn.prepareCall("{ call my_pack.getMyCursor(?) }");
cs.registerOutParameter(1,OracleTypes.CURSOR);
cs.execute();
ResultSet rs = ((OracleCallableStatement)cs).getCursor(1);
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+ rs.getDate(4)+" "+rs.getString(5));
}

3、Sql代码
--PL/SQL Code(存储过程) 带游标的OUT参数,返回游标(ref cursor)
create or replace procedure retCursor(ret_cursor out sys_refcursor)is
ret_cursor_value sys_refcursor;
begin
open ret_cursor_value for select * from student;
ret_cursor:=ret_cursor_value;
end retCursor;

--PL/SQL Code(存储过程) 带游标的OUT参数,返回游标(ref cursor)
create or replace procedure retCursor(ret_cursor out sys_refcursor)is
ret_cursor_value sys_refcursor;
begin
open ret_cursor_value for select * from student;
ret_cursor:=ret_cursor_value;
end retCursor;

4、Java代码
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@XX.XX.XX.XXX:XXXX:oracle9i","XXX_temp","XXX_temp");

DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
CallableStatement cs = conn.prepareCall("{ call retCursor(?) }");
cs.registerOutParameter(1,OracleTypes.CURSOR);
cs.execute();
ResultSet rs = ((OracleCallableStatement)cs).getCursor(1);
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+ rs.getDate(4)+" "+rs.getString(5));
}

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@XX.XX.XX.XXX:XXXX:oracle9i","XXX_temp","XXX_temp");

DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
CallableStatement cs = conn.prepareCall("{ call retCursor(?) }");
cs.registerOutParameter(1,OracleTypes.CURSOR);
cs.execute();
ResultSet rs = ((OracleCallableStatement)cs).getCursor(1);
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+ rs.getDate(4)+" "+rs.getString(5));
}

5、下面是个每个学生求平均值的存储过程。遇到的问题是带参数游标中的变量名字不要和表中的一样,否则会出问题

Sql代码
create or replace procedure AAA
as
--查询学生表的ID
cursor s_sno is select s.sno from student s;
--通过学生ID查询平均成绩
cursor sc_avg(s_no varchar2) is select avg(sc.degree) from score sc where sc.sno=s_no;
s_sno_j student.sno%type; --变量ID
sc_avg_i score.degree%type; --变量平局成绩
begin
open s_sno;--打开查询ID的游标
loop
fetch s_sno into s_sno_j;
exit when s_sno%notfound;
open sc_avg(s_sno_j); --打开查询平均成绩的游标,参数为学生ID
loop
fetch sc_avg into sc_avg_i;
exit when sc_avg%notfound;
dbms_output.put_line(sc_avg_i);
end loop;
close sc_avg;
end loop;
close s_sno;
end AAA;
追问
好多。。
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式