Explicit Cursor
--====================================================
--Explicit Cursor
/*
An explicit
cursor is a session cursor that you
construct and manage.
You must declare
and define an explicit cursor, giving it a name and associating it with a query
(typically, the query returns multiple rows).
we can
process Explicit cursor data by using two ways as below
1. Open the
explicit cursor (with the OPEN statement), fetch rows from the result set (with
the FETCH statement), and close the explicit cursor (with the CLOSE statement).
2. Use the
explicit cursor in a cursor FOR LOOP statement
*/
EMPNO
|
ENAME
|
HIREDATE
|
DOB
|
GENDER
|
SAL
|
DEPTNO
|
CREATION_DATE
|
JOB
|
COMM
|
1000
|
Sachin
|
11-Sep-85
|
01-Mar-20
|
M
|
25000
|
20
|
21-04-2020 18:46
|
MANAGER
|
|
1002
|
Kumar
|
01-Aug-82
|
11-Mar-20
|
M
|
125000
|
20
|
21-04-2020 18:46
|
|
|
1008
|
Sonu
|
11-Sep-85
|
01-Mar-20
|
M
|
2500
|
10
|
21-04-2020 18:46
|
CLERK
|
|
1003
|
Chavi
|
25-Mar-81
|
15-Feb-20
|
F
|
5000
|
20
|
21-04-2020 18:46
|
CLERK
|
|
1006
|
Ravi
|
11-Sep-89
|
01-Mar-19
|
M
|
2500
|
10
|
21-04-2020 18:46
|
CLERK
|
|
1005
|
Vinod
|
18-Sep-86
|
01-Mar-20
|
M
|
17000
|
30
|
21-04-2020 18:46
|
DEVELOPER
|
|
1004
|
Neel
|
28-Jan-88
|
19-Jul-19
|
M
|
27000
|
30
|
21-04-2020 18:46
|
MANAGER
|
|
1007
|
Manoj
|
11-Sep-85
|
09-Mar-18
|
M
|
18000
|
10
|
21-04-2020 18:46
|
DEVELOPER
|
|
1001
|
Amit
|
11-Sep-80
|
01-Jan-20
|
M
|
10000
|
40
|
21-04-2020 18:46
|
DEVELOPER
|
|
--Example:-
--============================================
--- DECLARE /
OPEN / FETCH / CLOSE ---- without loop fetch statement ----
DECLARE
v_name XX_EMP_TL.ENAME%TYPE;
CURSOR C1 IS
SELECT ename FROM XX_EMP_TL ;
BEGIN
OPEN C1;
FETCH C1 INTO
v_name; DBMS_OUTPUT.PUT_LINE (C1%ROWCOUNT||' - '||v_name);
FETCH C1 INTO
v_name; DBMS_OUTPUT.PUT_LINE (C1%ROWCOUNT||' - '||v_name);
FETCH C1 INTO
v_name; DBMS_OUTPUT.PUT_LINE (C1%ROWCOUNT||' - '||v_name);
FETCH C1 INTO
v_name; DBMS_OUTPUT.PUT_LINE (C1%ROWCOUNT||' - '||v_name);
FETCH C1 INTO
v_name; DBMS_OUTPUT.PUT_LINE (C1%ROWCOUNT||' - '||v_name);
FETCH C1 INTO
v_name; DBMS_OUTPUT.PUT_LINE (C1%ROWCOUNT||' - '||v_name);
FETCH C1 INTO
v_name; DBMS_OUTPUT.PUT_LINE (C1%ROWCOUNT||' - '||v_name);
FETCH C1 INTO
v_name; DBMS_OUTPUT.PUT_LINE (C1%ROWCOUNT||' - '||v_name);
FETCH C1 INTO
v_name; DBMS_OUTPUT.PUT_LINE (C1%ROWCOUNT||' - '||v_name);
FETCH C1 INTO
v_name; DBMS_OUTPUT.PUT_LINE (C1%ROWCOUNT||' - '||v_name);
FETCH C1 INTO
v_name; DBMS_OUTPUT.PUT_LINE (C1%ROWCOUNT||' - '||v_name);
FETCH C1 INTO
v_name; DBMS_OUTPUT.PUT_LINE (C1%ROWCOUNT||' - '||v_name);
FETCH C1 INTO
v_name; DBMS_OUTPUT.PUT_LINE (C1%ROWCOUNT||' - '||v_name);
CLOSE C1;
END;
OUTPUT:-
1 -
Sachin
2 - Rena
3 - Sonu
4 -
Chavi
5 - Ravi
6 -
Vinod
7 - Rani
8 -
Manoj
9 - Amit
10 -
Nitin Kumar
10 -
Nitin Kumar
10 -
Nitin Kumar
10 -
Nitin Kumar
--==========================
Single Column =================
DECLARE
v_name XX_EMP_TL.ENAME%TYPE;
CURSOR C1 IS
SELECT ename FROM XX_EMP_TL ;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO
v_name;
EXIT WHEN C1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE
(C1%ROWCOUNT||' - '||v_name);
END LOOP;
CLOSE C1;
END;
OUTPUT:-
1 -
Sachin
2 - Rena
3 - Sonu
4 -
Chavi
5 - Ravi
6 -
Vinod
7 - Rani
8 -
Manoj
9 - Amit
10 -
Nitin Kumar
--==========================
Duble Column =================
DECLARE
v_name XX_EMP_TL.ENAME%TYPE;
V_Sal XX_EMP_TL.SAL%TYPE;
CURSOR C1 IS
SELECT ename, sal FROM XX_EMP_TL ;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO
v_name,
V_Sal;
EXIT WHEN C1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE
(C1%ROWCOUNT||' - '||v_name||' |
salary is :- '||V_Sal);
END LOOP;
CLOSE C1;
END;
OUTPUT:-
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
--==================Full Table Column Used ========================
DECLARE
V_EMP XX_EMP_TL%ROWTYPE;
CURSOR C1 IS
SELECT * FROM XX_EMP_TL ;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO
V_EMP;
EXIT WHEN C1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE (C1%ROWCOUNT||' - '||V_EMP.ename);
END LOOP;
CLOSE C1;
END;
OUTPUT:-
1 -
Sachin
2 - Rena
3 - Sonu
4 -
Chavi
5 - Ravi
6 -
Vinod
7 - Rani
8 -
Manoj
9 - Amit
10 -
Nitin Kumar
--====================================================
--- CURSOR FOR
LOOP ----
--====================================================
DECLARE
CURSOR C1 IS
SELECT * FROM XX_EMP_TL ;
BEGIN
FOR I IN
C1
LOOP
DBMS_OUTPUT.PUT_LINE
(C1%ROWCOUNT||' - '||I.ename||' - '||I.SAL);
END LOOP;
END;
OUTPUT:-
1 -
Sachin - 25000
2 - Rena
- 125000
3 - Sonu
- 2500
4 -
Chavi - 5000
5 - Ravi
- 2500
6 -
Vinod - 17000
7 - Rani
- 27000
8 -
Manoj - 18000
9 - Amit
- 10000
10 -
Nitin Kumar - 21000
Comments
Post a Comment