Cursor

Features Types

There are the following two types of Cursors:
  1. Implicit Cursors
  2. Explicit Cursors

Implicit Cursors

Cursor Attributes

Attributes Working
% IS OPEN Returns BOOLEAN value
Evaluate to TRUE IF the cursor is open else evaluates to FALSE
%FOUND Returns BOOLEAN value
Evaluates to TRUE IF the most resent fetch returns a row else evaluates to FALSE
%NOTFOUND Returns BOOLEAN value
Evaluates to TRUE IF the most resent fetch does not return a row else evaluates to FALSE
%ROWCOUNT Returns a number
Evaluates the total number of row returned so far by the cursor.

Create Table

  1. CREATE TABLE Employee_Detail
  2. (
  3. EMP_ID NUMBER, EMP_NAME VARCHAR(50), Designation VARCHAR(50), SALARY NUMBER
  4. )
Insert Records
  1. insert into Employee_Detail values(100, 'Rupesh Kahane', 'CEO', 30000)
  2. insert into Employee_Detail values(101, 'Vaibhav Taware', 'Developer', 14000)
  3. insert into Employee_Detail values(102, 'Ajit Katte', 'Sales', 18000)
  4. insert into Employee_Detail values(103, 'Vaibhav Taware', 'B Developer', 17000)
  5. insert into Employee_Detail values(104, 'Mitesh M', 'Tester', 9000)
  6. insert into Employee_Detail values(105, 'Abhijit', 'Team Lead', 20000)
  7. insert into Employee_Detail values(106, 'Nikhil G', 'Developer', 20000)
Declare
  1. v_Designation Employee_detail.Designation%Type := 'P LEAD';
  2. BEGIN
  3. Update Employee_detail Set Designation ='Developer'
  4. where Designation =v_Designation;
  5. DBMS_Output.PUT_LINE(SQL%ROWCOUNT || ' Rows are Updated');
  6. IF SQL%NOTFOUND THEN
  7. DBMS_Output.PUT_LINE('Data not found...Process Terminated');
  8. END IF;
  9. END;
Output

Oracle Table
Explicit Cursor

Advantages

There are a few steps that you need to follow:

  1. Cursor Declaration

    A Cursor is declared in the declaration block and provided with a Name and a SELECT statement.

  2. Cursor Opening

    To process any cursor for the business logic, it is mandatory that it should be opened. This enables creation of a Context Area.

  3. Cursor Fetching

    Fetching retrieves records from the Context Area into a variable such that the variable can be used for the business logic.
    A Fetch command operates on the current record only and processes through the result set one record at a time.

  4. Cursor Closing

    The cursor should be closed else it may lead to a memory leak. Until the cursor is closed, the memory is not released.

Syntax

DECLARE
Cursor Declaration
BEGIN
Opening of Cursor
Fetching of Cursor
Close Cursor
End;

Cursor with “Simple loop”

  1. DECLARE
    v_EMP_ID Employee_detail.EMP_ID%TYPE;
  2. CURSOR employee_cursor
  3. IS
  4. SELECT EMP_ID
  5. FROM Employee_detail;
  6. BEGIN
    OPEN employee_cursor;
  7. FETCH employee_cursor INTO v_EMP_ID;
  8. DBMS_Output.PUT_LINE('EMPLOYEE ID ' || v_EMP_ID);
  9. FETCH employee_cursor INTO v_EMP_ID;
  10. DBMS_Output.PUT_LINE('EMPLOYEE ID ' || v_EMP_ID);
  11. FETCH employee_cursor INTO v_EMP_ID;
  12. DBMS_Output.PUT_LINE('EMPLOYEE ID ' || v_EMP_ID);
  13. CLOSE employee_cursor;
  14. END;
Output

Oracle Table record

Cursor with “loop”
  1. DECLARE
  2. CURSOR employee_cursor
  3. IS
  4. SELECT * FROM Employee_detail;
  5. v_Emp_Data employee_cursor%ROWTYPE;
  6. BEGIN
  7. OPEN employee_cursor;
  8. LOOP
  9. FETCH employee_cursor INTO v_Emp_Data;
  10. EXIT WHEN employee_cursor%NOTFOUND;
  11. DBMS_Output.PUT_LINE(' Emp Id: ' || v_Emp_Data.Emp_ID);
  12. DBMS_Output.PUT_LINE(' Name: ' ||v_Emp_Data.Emp_Name);
  13. DBMS_Output.PUT_LINE('');
  14. END LOOP;
  15. CLOSE employee_cursor;
  16. END;
Output

Oracle query output

In the above example within the loop each record in the Active Set is retrieved and used.

Program To print employee details
  1. DECLARE
  2. CURSOR employee_cursor
  3. IS
  4. SELECT * FROM Employee_detail;
  5. v_Emp_Data employee_cursor%ROWTYPE;
  6. BEGIN
  7. OPEN employee_cursor;
  8. DBMS_Output.PUT_LINE(' Employee Details are as follows');
  9. DBMS_Output.PUT_LINE('--------------------------------------------------------------');
  10. DBMS_Output.PUT_LINE('Emp Id '||' Name '||' Organization '||'Salary ');
  11. DBMS_Output.PUT_LINE('--------------------------------------------------------------');
  12. LOOP
  13. FETCH employee_cursor INTO v_Emp_Data;
  14. EXIT WHEN employee_cursor%NOTFOUND;
  15. DBMS_Output.PUT_LINE(RPAD(v_Emp_Data.Emp_ID,15)|| RPAD(v_Emp_Data.Emp_Name,15)|| LPAD(v_Emp_Data.Designation,15)|| LPAD(v_Emp_Data.Salary,15));
  16. DBMS_Output.PUT_LINE('');
  17. END LOOP;
  18. CLOSE employee_cursor;
  19. END;
Output

table of employee detail

Cursor with “While Loop”
  1. DECLARE
  2. CURSOR employee_cursor
  3. IS
  4. SELECT *
  5. FROM Employee_detail;
  6. v_EMP_DATA employee_cursor%ROWTYPE;
  7. BEGIN
  8. OPEN employee_cursor;
  9. FETCH employee_cursor INTO v_EMP_DATA;
  10. WHILE employee_cursor%FOUND
  11. LOOP
  12. DBMS_Output.PUT_LINE('Employee Name is : ' || v_EMP_DATA.EMP_NAME);
  13. FETCH employee_cursor INTO v_EMP_DATA;
  14. END LOOP;
  15. CLOSE employee_cursor;
  16. END;
Output

employee table in oracle

Cursor with “FOR Loop”

  1. DECLARE
  2. CURSOR employee_cursor
  3. IS
  4. SELECT *
  5. FROM Employee_detail;
  6. v_EMP_DATA employee_cursor%ROWTYPE;
  7. BEGIN
  8. FOR v_EMP_DATA IN employee_cursor
  9. LOOP
  10. DBMS_Output.PUT_LINE('Employee Id is :' || v_EMP_DATA.EMP_Id);
  11. DBMS_Output.PUT_LINE('Employee Name is :' || v_EMP_DATA.EMP_NAME );
  12. DBMS_Output.PUT_LINE('Designation is :' || v_EMP_DATA.Designation );
  13. DBMS_Output.PUT_LINE('Employee Salary is :' || v_EMP_DATA.Salary);
  14. DBMS_Output.PUT_LINE('' );
  15. END LOOP;
  16. END;
Output

employee information in oracle


Conclusion

I hope that this article will be very useful for beginners to understand cursors. I welcome all your suggestions and comments.