2. Definition
• A cursor is a temporary work area created in
the system memory when a SQL statement is
executed. A cursor contains information on a
select statement and the rows of data
accessed by it.
• Can hold information of more than one row.
• One row at a time can be accessed.
4. Implicit Cursor
• Oracle internally creates Implicit cursor
• Whenever a DML operation like
SELECT,UPDATE,INSERT happens on a row
Oracle creates the cursor internally.
• SELECT on a single row is a implicit cursor.
• Status of the Implicit cursor can be found
using SQL%.
• Use cannot specify a name to Implicit Cursor.
5. Implicit Cursor
SQL%FOUND The return value is TRUE, if the DML
statements like INSERT, DELETE and
UPDATE affect at least one row and if
SELECT ….INTO statement return at least
one row.
SQL%ROWCOUNT The return value is FALSE, if DML
statements like INSERT, DELETE and
UPDATE at least one row and if SELECT
….INTO statement return at least one row
SQL%NOTFOUND Return the number of rows affected by
the DML operations INSERT, DELETE,
UPDATE, SELECT
6. Explicit Cursor
• User defines the Explicit Cursor
• Involves specific operations to use the cursor.
• Has a definite name.
• Multiple rows are selected using Explicit
Cursor
7. Defining Explicit Cursor
Declare
CURSOR cur_xmp_cursor IS
(SELECT * from emplyees_tab);
Begin
----<other code>
Exception
---<exception handling>
END:
Cursor Name
Table Name
9. Operations Explained.
DECLARE
CURSOR curr_exp_examp IS
(SELECT * FROM employees_tab);
L_employee_row employees_tab%ROWTYPE;
BEGIN
OPEN curr_exp_examp ;
LOOP
FETCH curr_exp_examp INTO L_employee_row ;
< CODE TO PROCESS>
EXIT WHEN curr_exp_examp %NOTFOUND;
END LOOP;
CLOSE curr_exp_examp ;
END;
10. Example
• DECLARE
• CURSOR CUR_EXP_EXAMP IS(
• SELECT *
• FROM EMPLOYEES);
• L_EMP_ROW EMPLOYEES%ROWTYPE; ---- OR L_EMP_ROW CUR_EXP_EXAMP%ROWTYPE;
• BEGIN
• OPEN CUR_EXP_EXAMP;
• LOOP
• FETCH CUR_EXP_EXAMP
• INTO L_EMP_ROW;
• DBMS_OUTPUT.PUT_LINE('employee name->' || L_EMP_ROW.EMP_NAME);
• EXIT WHEN CUR_EXP_EXAMP%NOTFOUND;
• END LOOP;
• CLOSE CUR_EXP_EXAMP;
• END;