Cursors in Oracle

Cursor

  • Oracle creates an area of memory called a Context Area to process SQL statements.
  • The context area includes:

    1. Number of rows processed by the statement.
    2. A Cursor is a pointer to a Context Area.

  • A SQL query result is called an Active set of records.
  • This temporary work area is used to store the data retrieved from the database and manipulate this data.
  • Once the complete result set is fetched, the context area is flushed out to release back the memory.
Features
  • A Cursor allows fetching and processing rows returned by a select statement and process one row at a time.
Types

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

Implicit Cursors

  • An Implicit Cursor that is automatically declared by an Oracle Server statement is executed
  • User/Programmer cannot control the information in this cursor
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 
  • An Explicit Cursor is defined within the program for a query that returns more than one row of data.
  • This cursor allows sequential processing of each row of the returned data from the database.
  • An Explicit Cursor is declared using Name with an association of a SELECT statement in the DECLARE section of the PL/SQL block.

Advantages

  • Provides programmatic control for programmers.
  • Easy to trap errors.
  • Designed to work with SELECT statements that return more than one record at a time.
  • Require additional steps to operate than an Implicit Cursor.

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.

  • An EXIT WHEN statement is mandatory to be the part of the loop.
  • A loop is terminated with a proper cursor attribute operating on %NOTFOUND status.
  • A loop is terminated when the condition is TRUE.
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”
  • The loop works on a range oriented operational logic.
  • The loop is very useful for traveling through all the data in a database table.
  • This is more dynamic in operation than a simple loop.
  • The cursor does not require an explicit OPEN, FETCH and CLOSE.

 

  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.


Similar Articles