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();
}
}
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();
}
}
댓글 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
86 | sql 문 실행명령 | 원우석 | 2004.07.31 | 3500 |
85 | DB2 종료후 다시 시작 | 원우석 | 2004.07.31 | 2660 |
84 | lock 상태 확인 | 원우석 | 2004.07.31 | 4803 |
83 | sybase DB에서 파일로 백업을 받구 그파일을 다시 DB2로 임포트하는방법 | 원우석 | 2004.07.31 | 3748 |
82 | 임포트시 테이블에 데이터를 모두 클리어 할때 | 원우석 | 2004.07.31 | 2793 |
81 | 특정테이블을 백업받을때 | 원우석 | 2004.07.31 | 3093 |
80 | DB를 백업받을때 | 원우석 | 2004.07.31 | 2811 |
79 | 카탈로그 파일 내용보기 | 원우석 | 2004.07.31 | 2988 |
78 | 데이타 모델링(이화식 강의) 부분 목차정리. | 박상현 | 2004.06.22 | 3124 |
77 | oracle 8i제공 explan table 생성 script | 박상현 | 2004.05.08 | 2493 |
76 | toad에서 사용가능한 explan_table생성 script | 박상현 | 2004.05.08 | 6381 |
75 | 오라클 DUAL Table처럼 DB2에서 임시 테이블 사용하기 | 원우석 | 2004.01.08 | 4213 |
74 | ORACLE -> MSSQL 시 LPAD 처리 | 운영자 | 2003.10.08 | 4050 |
73 | Unix, Oracle, Pro*C작성 입찰공고 | 운영자 | 2003.06.05 | 4354 |
72 | Unix, Oracle, Pro*C작성 구인구직게시판 | 운영자 | 2003.06.05 | 4729 |
71 | Unix, Oracle, Pro*C작성 자유게시판 | 운영자 | 2003.06.05 | 4595 |
70 | plan table(sql실행계획) 보는 select문 | 운영자 | 2003.01.28 | 3831 |
69 | java에서 array처리 방법 | 운영자 | 2003.01.27 | 6033 |
68 | 효율적인 25가지 SQL작성법 | 운영자 | 2003.01.04 | 2911 |
67 | 중복 data는 처음에만 display 하게 하는 방법 | 운영자 | 2002.11.23 | 2852 |