oracle存储过程调用返回结果,调用Oracle的存储过程返回结果集

With 7.2 on up of the database you have cursor variables.  Cursor variables are cursors opened by a pl/sql routine and fetched from by another application or pl/sql routine (in 7.3 pl/sql routines can fetch from cursor variables as well as open them). The cursor variables are opened with the privelegs of the owner of the procedure and behave just like they were completely contained within the pl/sql routine. It uses the inputs to decide what database it will run a query on.

Here is an example:

create or replace package types

as

type cursorType is ref cursor;

end;

/

create or replace function sp_ListEmp return types.cursortype

as

l_cursor    types.cursorType;

begin

open l_cursor for select ename, empno from emp order by ename;

return l_cursor;

end;

/create or replace procedure getemps( p_cursor in out types.cursorType )

as

begin

open p_cursor for select ename, empno from emp order by ename;

end;

/

examples for SQLPlus, Pro*C, Java/JDBC, ODBC, ADO/ASP, DBI Perl and OCI follow:

REM SQL*Plus commands to use a cursor variable

variable c refcursor

exec :c := sp_ListEmp

print c

exec getEmps( :c )

print c

and the Pro*C to use this would look like:

static void process()

{

EXEC SQL BEGIN DECLARE SECTION;

SQL_CURSOR  my_cursor;

VARCHAR     ename[40];

int         empno;

EXEC SQL END DECLARE SECTION;

EXEC SQL WHENEVER SQLERROR DO sqlerror_hard();

EXEC SQL ALLOCATE :my_cursor;

EXEC SQL EXECUTE BEGIN

:my_cursor := sp_listEmp;

END; END-EXEC;

for( ;; )

{

EXEC SQL WHENEVER NOTFOUND DO break;

EXEC SQL FETCH :my_cursor INTO :ename, empno;

printf( "'%.*s', %d"n", ename.len, ename.arr, empno );

}

EXEC SQL CLOSE :my_cursor;

}

And the java to use this could be:

import java.sql.*;

import java.io.*;

import oracle.jdbc.driver.*;

class curvar

{

public static void main (String args [])

throws SQLException, ClassNotFoundException

{

String driver_class = "oracle.jdbc.driver.OracleDriver";

String connect_string = "jdbc:oracle:thin:@slackdog:1521:oracle8";

String query = "begin :1 := sp_listEmp; end;";

Connection conn;

Class.forName(driver_class);

conn = DriverManager.getConnection(connect_string, "scott", "tiger");

CallableStatement cstmt = conn.prepareCall(query);

cstmt.registerOutParameter(1,OracleTypes.CURSOR);

cstmt.execute();

ResultSet rset = (ResultSet)cstmt.getObject(1);

while (rset.next ())

System.out.println( rset.getString (1) );

cstmt.close();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值