ResultSetsfromStoredProceduresInOracle

来源:Oracle认证    发布时间:2012-11-12    Oracle认证视频    评论

  Result Sets from Stored Procedures In Oracle
  A frequently asked question is:

  I'd like to know whether ORACLE supports procedures (functions) which
  returns result sets.

  The answer is most definitely yes.  In short, it'll look like this:
 
  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;
  /
  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;
  /
  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

  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();
    }
  }

 

  The following is thanks to marktoml@hotmail.com (mark tomlinson).. 
 

上一页123下一页

视频学习

我考网版权与免责声明

① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;

② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。

最近更新

社区交流

考试问答