types of Ref Cursor
EMPNO
|
ENAME
|
HIREDATE
|
DOB
|
GENDER
|
SAL
|
DEPTNO
|
JOB
|
1000
|
Sachin
|
11-Sep-85
|
01-Mar-20
|
M
|
25000
|
20
|
MANAGER
|
1002
|
Rena
|
01-Aug-82
|
11-Mar-20
|
F
|
125000
|
20
|
|
1008
|
Sonu
|
11-Sep-85
|
01-Mar-20
|
M
|
2500
|
10
|
CLERK
|
1003
|
Chavi
|
25-Mar-81
|
15-Feb-20
|
F
|
5000
|
20
|
CLERK
|
1006
|
Ravi
|
11-Sep-89
|
01-Mar-19
|
M
|
2500
|
10
|
CLERK
|
1005
|
Vinod
|
18-Sep-86
|
01-Mar-20
|
M
|
17000
|
30
|
DEVELOPER
|
1004
|
Rani
|
28-Jan-88
|
19-Jul-19
|
F
|
27000
|
30
|
MANAGER
|
1007
|
Manoj
|
11-Sep-85
|
09-Mar-18
|
M
|
18000
|
10
|
DEVELOPER
|
1001
|
Amit
|
11-Sep-80
|
01-Jan-20
|
M
|
10000
|
40
|
DEVELOPER
|
1009
|
Nitin
Kumar
|
11-Sep-85
|
11-Mar-20
|
M
|
21000
|
20
|
DEVELOPER
|
--===================================================
Types of Ref Cursor
1.
Strong type :- we explain the return type here.
2. WeaK
type :- No need
to explain the return type here.
--====================================================
--How to declare
REF Cursor
--====================================================
DECLARE
TYPE
XX_STRONG_REF IS REF CURSOR RETURN
employees%ROWTYPE; --
strong type
C_EMP XX_STRONG_REF; --
strong cursor variable
TYPE
XX_WEAK_REF IS REF CURSOR; --
weak type
C_EMP XX_WEAK_REF; --
weak cursor variable
C_EMP SYS_REFCURSOR; --
weak cursor variable
BEGIN
NULL;
END;
--================
EXAMPLE :- --strong
type
--================
DECLARE
TYPE
XX_STRONG_REF IS REF CURSOR RETURN XX_EMP_TL%ROWTYPE; --
strong type
C_S_REF XX_STRONG_REF; --
strong cursor variable
VAR_EMP XX_EMP_TL%ROWTYPE;
VAR_DEPT XX_DEPT_TL%ROWTYPE;
BEGIN
dbms_output.put_line ('**************
EMPLOYEE DETAILS****************');
OPEN
C_S_REF FOR SELECT * FROM XX_EMP_TL ;
LOOP
FETCH
C_S_REF INTO
VAR_EMP;
EXIT WHEN
C_S_REF%NOTFOUND;
DBMS_OUTPUT.PUT_LINE
(C_S_REF%ROWCOUNT||' - '||VAR_EMP.ENAME||' |
salary is :- '||VAR_EMP.SAL);
END LOOP;
CLOSE
C_S_REF;
-- dbms_output.put_line ('**************
DEPARTMENT DETAILS****************');
-- OPEN C_S_REF FOR SELECT * FROM
XX_DEPT_TL;
-- LOOP
-- FETCH C_S_REF INTO VAR_DEPT;
-- EXIT WHEN C_S_REF%NOTFOUND;
-- DBMS_OUTPUT.PUT_LINE
(C_S_REF%ROWCOUNT||' - '||VAR_DEPT.DNAME);
-- END LOOP;
-- CLOSE C_S_REF;
END;
OUTPUT:-
**************
EMPLOYEE DETAILS****************
1 -
Sachin | salary is :- 25000
2 - Rena
| salary is :- 125000
3 - Sonu
| salary is :- 2500
4 -
Chavi | salary is :- 5000
5 - Ravi
| salary is :- 2500
6 -
Vinod | salary is :- 17000
7 - Rani
| salary is :- 27000
8 -
Manoj | salary is :- 18000
9 - Amit
| salary is :- 10000
10 -
Nitin Kumar | salary is :- 21000
--================
EXAMPLE :- --Weak
type
--================
DECLARE
TYPE
XX_WEAK_REF IS REF CURSOR ; --
Weak type
C_W_REF XX_WEAK_REF; --
weak cursor variable
VAR_EMP XX_EMP_TL%ROWTYPE;
VAR_DEPT XX_DEPT_TL%ROWTYPE;
BEGIN
dbms_output.put_line ('**************
EMPLOYEE DETAILS****************');
OPEN
C_W_REF FOR SELECT * FROM XX_EMP_TL ;
LOOP
FETCH
C_W_REF INTO
VAR_EMP;
EXIT WHEN
C_W_REF%NOTFOUND;
DBMS_OUTPUT.PUT_LINE
(C_W_REF%ROWCOUNT||' - '||VAR_EMP.ENAME||' |
salary is :- '||VAR_EMP.SAL);
END LOOP;
CLOSE
C_W_REF;
dbms_output.put_line ('**************
DEPARTMENT DETAILS****************');
OPEN
C_W_REF FOR SELECT * FROM XX_DEPT_TL;
LOOP
FETCH
C_W_REF INTO
VAR_DEPT;
EXIT WHEN
C_W_REF%NOTFOUND;
DBMS_OUTPUT.PUT_LINE
(C_W_REF%ROWCOUNT||' - '||VAR_DEPT.DNAME);
END LOOP;
CLOSE
C_W_REF;
END;
OUTPUT:-
**************
EMPLOYEE DETAILS****************
1 -
Sachin | salary is :- 25000
2 - Rena
| salary is :- 125000
3 - Sonu
| salary is :- 2500
4 -
Chavi | salary is :- 5000
5 - Ravi
| salary is :- 2500
6 -
Vinod | salary is :- 17000
7 - Rani
| salary is :- 27000
8 -
Manoj | salary is :- 18000
9 - Amit
| salary is :- 10000
10 -
Nitin Kumar | salary is :- 21000
**************
DEPARTMENT DETAILS****************
1 -
ACCOUNTING
2 -
RESEARCH
3 -
SALES
4 - OPERATIONS
DECLARE
--
SYS_REFCURSOR System defind ref type
----
--TYPE
XX_WEAK_REF IS REF CURSOR ; -- Weak type
C_W_REF SYS_REFCURSOR; --
weak cursor variable
VAR_EMP XX_EMP_TL%ROWTYPE;
VAR_DEPT XX_DEPT_TL%ROWTYPE;
BEGIN
dbms_output.put_line ('**************
EMPLOYEE DETAILS****************');
OPEN
C_W_REF FOR SELECT * FROM XX_EMP_TL ;
LOOP
FETCH
C_W_REF INTO
VAR_EMP;
EXIT WHEN
C_W_REF%NOTFOUND;
DBMS_OUTPUT.PUT_LINE
(C_W_REF%ROWCOUNT||' - '||VAR_EMP.ENAME||' |
salary is :- '||VAR_EMP.SAL);
END LOOP;
CLOSE
C_W_REF;
dbms_output.put_line ('**************
DEPARTMENT DETAILS****************');
OPEN
C_W_REF FOR SELECT * FROM XX_DEPT_TL;
LOOP
FETCH
C_W_REF INTO
VAR_DEPT;
EXIT WHEN
C_W_REF%NOTFOUND;
DBMS_OUTPUT.PUT_LINE
(C_W_REF%ROWCOUNT||' - '||VAR_DEPT.DNAME);
END LOOP;
CLOSE
C_W_REF;
END;
OUTPUT:-
**************
EMPLOYEE DETAILS****************
1 -
Sachin | salary is :- 25000
2 - Rena
| salary is :- 125000
3 - Sonu
| salary is :- 2500
4 -
Chavi | salary is :- 5000
5 - Ravi
| salary is :- 2500
6 -
Vinod | salary is :- 17000
7 - Rani
| salary is :- 27000
8 -
Manoj | salary is :- 18000
9 - Amit
| salary is :- 10000
10 -
Nitin Kumar | salary is :- 21000
**************
DEPARTMENT DETAILS****************
1 -
ACCOUNTING
2 -
RESEARCH
3 -
SALES
4 - OPERATIONS
--====================================================
--Example Cursor Variable with User-Defined Return Type
--====================================================
DECLARE
TYPE
XX_EMP_TYPE IS RECORD (
EMPNO NUMBER,
ENAME VARCHAR2(100),
SAL NUMBER);
TYPE
XX_STRONG_REF IS REF CURSOR RETURN
XX_EMP_TYPE; --
strong type
C_S_REF XX_STRONG_REF; --
strong cursor variable
VAR_EMP
XX_EMP_TYPE;
BEGIN
dbms_output.put_line ('**************
EMPLOYEE DETAILS****************');
OPEN
C_S_REF FOR SELECT EMPNO,
ENAME, SAL FROM XX_EMP_TL ;
LOOP
FETCH
C_S_REF INTO
VAR_EMP;
EXIT WHEN
C_S_REF%NOTFOUND;
DBMS_OUTPUT.PUT_LINE
(C_S_REF%ROWCOUNT||' - '||VAR_EMP.ENAME||' |
salary is :- '||VAR_EMP.SAL);
END LOOP;
CLOSE
C_S_REF;
END;
OUTPUT:-
**************
EMPLOYEE DETAILS****************
1 -
Sachin | salary is :- 25000
2 - Rena
| salary is :- 125000
3 - Sonu
| salary is :- 2500
4 -
Chavi | salary is :- 5000
5 - Ravi
| salary is :- 2500
6 -
Vinod | salary is :- 17000
7 - Rani
| salary is :- 27000
8 -
Manoj | salary is :- 18000
9 - Amit
| salary is :- 10000
10 -
Nitin Kumar | salary is :- 21000
Comments
Post a Comment