Doubt On Ref Cursor

37 views
Skip to first unread message

Nihar Sahoo

unread,
Jun 19, 2015, 2:24:29 PM6/19/15
to oracle_db...@googlegroups.com
I know the use of Explicit cursor. But when should i use ref cursor.


ddf

unread,
Jun 19, 2015, 4:12:33 PM6/19/15
to oracle_db...@googlegroups.com, niha...@gmail.com


On Friday, June 19, 2015 at 12:24:29 PM UTC-6, Nihar Sahoo wrote:
I know the use of Explicit cursor. But when should i use ref cursor.




It's usually used to transfer data sets from one program to another; a simple example of using a ref cursor is shown:

 SQL> create or replace function get_tab_data(tabname in varchar2)
  2  return sys_refcursor as
  3
  4          mycur sys_refcursor;
  5
  6          sqlstr varchar2(4000);
  7
  8  begin
  9          sqlstr := 'select * from '||tabname;
 10
 11          open mycur for sqlstr;
 12
 13          return mycur;
 14
 15  end;
 16  /
Function created.
SQL>
SQL> show errors function get_tab_data
No errors.
SQL>
SQL> select get_tab_data('EMP') from dual;
GET_TAB_DATA('EMP')
--------------------
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7788 SCOTT      ANALYST         7566 09-DEC-82       3000                    20
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
      7876 ADAMS      CLERK           7788 12-JAN-83       1100                    20

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10
      7939 DUKE       CEO                  17-NOV-81       5000                    10
      7949 PRINCE     CFO                  17-NOV-81       5000                    10
      7959 QUEEN      CIO                  17-NOV-81       5000                    10
      7869 JACK       PRESIDENT            17-NOV-81       5000                    10

18 rows selected.

SQL>


David Fitzjarrell
Reply all
Reply to author
Forward
0 new messages