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


--====================================================
--Ref Cursor Requirement
--====================================================

DECLARE

VAR_1 VARCHAR2(100);
VAR_2 NUMBER;

  CURSOR C_EMP IS 
  SELECT ename, sal FROM XX_EMP_TL ;

  CURSOR C_DEPT IS 
  SELECT DNAME, DEPTNO FROM XX_DEPT_TL ;

    BEGIN
    dbms_output.put_line ('************** EMPLOYEE DETAILS****************');     
        OPEN C_EMP;
            LOOP               
                FETCH C_EMP INTO VAR_1, VAR_2;
                EXIT WHEN C_EMP%NOTFOUND;                                
                 DBMS_OUTPUT.PUT_LINE (C_EMP%ROWCOUNT||' - '||VAR_1||' | salary is :- '||VAR_2);                   
            END LOOP;           
        CLOSE C_EMP;     
        
        dbms_output.put_line ('************** DEPARTMENT DETAILS****************');
        OPEN C_DEPT;
            LOOP               
                FETCH C_DEPT INTO VAR_1, VAR_2;
                EXIT WHEN C_DEPT%NOTFOUND;                                
                 DBMS_OUTPUT.PUT_LINE (C_DEPT%ROWCOUNT||' - '||VAR_1||' | Location is :- '||VAR_2);                   
            END LOOP;           
        CLOSE C_DEPT;
       
    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 | Location is :- NEW YORK
2 - RESEARCH | Location is :- DALLAS
3 - SALES | Location is :- CHICAGO
4 - OPERATIONS | Location is :- BOSTON

--===================================================
Ref Cursor
--===================================================

DECLARE

Type XX_REF_CUR_TYPE IS REF CURSOR;

C_REF    XX_REF_CUR_TYPE;

VAR_1 VARCHAR2(100);
VAR_2 NUMBER;
 
    BEGIN
    dbms_output.put_line ('************** EMPLOYEE DETAILS****************');     
        OPEN C_REF FOR  SELECT ename, sal FROM XX_EMP_TL ;
            LOOP               
                FETCH C_REF INTO VAR_1, VAR_2;
                EXIT WHEN C_REF%NOTFOUND;                                
                 DBMS_OUTPUT.PUT_LINE (C_REF%ROWCOUNT||' - '||VAR_1||' | salary is :- '||VAR_2);                   
            END LOOP;           
        CLOSE C_REF;      
       
        dbms_output.put_line ('************** DEPARTMENT DETAILS****************');
        OPEN C_REF FOR SELECT DNAME, DEPTNO FROM XX_DEPT_TL;
            LOOP               
                FETCH C_REF INTO VAR_1, VAR_2;
                EXIT WHEN C_REF%NOTFOUND;                                
                 DBMS_OUTPUT.PUT_LINE (C_REF%ROWCOUNT||' - '||VAR_1||' | Location is :- '||VAR_2);                   
            END LOOP;           
        CLOSE C_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 | Location is :- NEW YORK
2 - RESEARCH | Location is :- DALLAS
3 - SALES | Location is :- CHICAGO
4 - OPERATIONS | Location is :- BOSTON




DECLARE

Type XX_REF_CUR_TYPE IS REF CURSOR;

C_REF XX_REF_CUR_TYPE;

VAR_EMP XX_EMP_TL%ROWTYPE;
VAR_DEPT XX_DEPT_TL%ROWTYPE;
 
    BEGIN
    dbms_output.put_line ('************** EMPLOYEE DETAILS****************');     
        OPEN C_REF FOR  SELECT * FROM XX_EMP_TL ;
            LOOP               
                FETCH C_REF INTO VAR_EMP;
                EXIT WHEN C_REF%NOTFOUND;                                
                 DBMS_OUTPUT.PUT_LINE (C_REF%ROWCOUNT||' - '||VAR_EMP.ENAME||' | salary is :- '||VAR_EMP.SAL);                   
            END LOOP;           
        CLOSE C_REF;     
       
        dbms_output.put_line ('************** DEPARTMENT DETAILS****************');
        OPEN C_REF FOR SELECT * FROM XX_DEPT_TL;
            LOOP               
                FETCH C_REF INTO VAR_DEPT;
                EXIT WHEN C_REF%NOTFOUND;                                
                 DBMS_OUTPUT.PUT_LINE (C_REF%ROWCOUNT||' - '||VAR_DEPT.DNAME);                   
            END LOOP;           
        CLOSE C_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

  

Comments

Popular posts from this blog

E-Text Report In Fusion | Types of E-Text reports

Supplier API's

How to pass default Parameter in cursor