메뉴 건너뛰기

tnt_db

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', %dn", 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();
  }
}
번호 제목 글쓴이 날짜 조회 수
74 TABLE,COLUMN,PROCEDURE 이름을 확인할때... 원우석 2006.03.10 3009
73 NESTED TABLE과 VARRAY사용법과 다른점 박상현 2005.11.03 8749
72 plan table(sql실행계획) 보는 select문 운영자 2003.01.28 3900
71 java에서 array처리 방법 운영자 2003.01.27 6101
70 효율적인 25가지 SQL작성법 운영자 2003.01.04 2933
69 중복 data는 처음에만 display 하게 하는 방법 운영자 2002.11.23 2881
68 중복 data를 찾아 한번만 display하는 방법 운영자 2002.11.23 3351
67 중복 data를 찾아 모두 display 하는 방법 운영자 2002.11.23 3012
» oracle의 procedure에서의 여러 리턴값의 활용 예 운영자 2002.10.18 8257
65 특정문자까지의 왼쪽 문자열 반환 function 운영자 2002.10.18 3949
64 중복자료 확인 및 삭제 운영자 2002.09.25 3204
63 RECORD단위 DATA를 COLUMN단위로 운영자 2002.09.18 3618
62 누계 COLUMN이 없는 TABLE에 누계 값을 보자 (부등호 JOIN) 운영자 2002.09.18 5037
61 바로이전 ROW의 값을 참조하고자 할때 운영자 2002.09.18 8546
60 누계 COLUMN이 없는 TABLE에 누계 값을 보자 (IN-LINE VIEW) 운영자 2002.09.18 3111
59 자기보다 작은값중 최대값 하나만 읽어오기 운영자 2002.09.18 4522
58 DATA COPY를 이용한 QUERY 운영자 2002.09.18 3136
57 PARAMETER값 변경에따른 유연한 GROUP BY 운영자 2002.09.18 4076
56 PAIRWISE 와 NONPAIRWISE 운영자 2002.09.18 2657
55 동일한 값 안보여주기 운영자 2002.09.18 2866
위로