Introduction to Cursor in Oracle
Cursors are very important while writing PL/SQL queries. In this article, we are going to discuss about Cursors in Oracle. So basically Cursor is a pointer to a context area. The context area is basically a memory space for processing an SQL statement. Thus context area contains all information which is required to process the statement. So, basically the cursor points to this particular area. On executing the statement the cursor holds the returned result set which can contain single or multiple rows. As we go on we will learn about cursor in more detail.
Cursor Types and Syntax
There are two types of cursors in Oracle. Implicit Cursors and Explicit Cursors.
1. Implicit Cursors
As the name suggests implicit cursors are created by oracle. Whenever an SQL statement is executed implicit cursor is created. DML statements like UPDATE, INSERT and DELETE automatically creates implicit cursor. Incase of UPDATE and INSERT DML statements the cursor holds the information of the rows to be affected and in case of INSERT it holds the information of the data which is to be inserted.
The whole execution of the implicit cursor is managed internally by oracle and it reveals the cursor status through certain attributes like %ROWCOUNT, %ISOPEN, %ISFOUND.
- %ROWCOUNT: It tells how many rows are returned by the cursor.
- %ISOPEN: This attribute returns Boolean which means TRUE if the cursor is open or else FALSE.
- %FOUND: This attribute also returns Boolean. TRUE if successful fetch has been executed and FALSE if there is unsuccessful fetch (no row returned).
- r%NOTFOUND: This attribute returns opposite of the %FOUND attribute. FALSE if the row is fetched and TRUE if no row is fetched.
2. Explicit Cursors
These cursors are created by the users in the oracle database. In this type of cursor, the programmers are allowed to create their own context area. In explicit cursor, the programmer gets their own context area to get more control to execute their DML statements. The explicit cursors are declared in the SELECT statement of the oracle database.
CURSOR <cursorname> IS <select_statement>;
FETCH <cursorname> INTO <cursorvariable>;
- cursor_name: It is used to give the name of the cursor.
- select_statement: Here we give the select query which returns single or multiple rows.
Cursor Action in Oracle
There are four actions that we need to use explicit cursors.
- DECLARE: In this section, the programmer actually creates a named context area for the Select statement. The select statement is declared in this section of the PL/SQL program.
- OPEN: In this section oracle actually allocates memory for the cursor.
- FETCH: In this section actual execution starts. The select statement fetches the records from the database and stores it in the allocated memory. The data is fetched record by record way. It is a record level activity. So the set of records is also called Active Set.
- CLOSE: This statement is used to close the cursor and the memory allocated to the cursor will be released.
Example of Cursor in Oracle
In this example, we are going to get the employee id from the employee table only for those employees who are from the city of Mumbai. We are going to use Cursor for this situation. Let us look at the PL/SQL block for displaying the id of the employees based in Mumbai.
SET SERVEROUTPUT ON;
CURSOR emp_cur IS
SELECT employee_id from employee WHERE CITY ='Mumbai';
FETCH emp_cur INTO v_empid;
EXIT WHEN emp_cur%NOTFOUND;
dbms_output.put_line('The employee id is' || v_empid);
The SET server output is used to allow the DBMS output command to print the messages in SQL Developer. Since we are going to use it for our execution. In the above PL/SQL block we have the first DECLARE section in which we declare the cursor and the cursor variable. In the next, we have the execution section where we OPEN the cursor which means the memory space has been allocated. The FETCH is used to execute the SELECT query and then store it in the cursor variable which we had declared.
We will use LOOP to transverse the records in the whole table and dbms_output.put_line statement to print the record after every iteration. The attribute %NOTFOUND is used to exit the LOOP when there are no records. The CLOSE cursor statement is used to close the cursor and release the memory. The END statement at the end is used at last to end the PL/SQL block. One important point to remember is that we need to close the cursor before the end statement and the LOOP should end before closing the cursor.
Let us now execute the PL/SQL cursor block in SQL developer and the below screenshot shows us the output after executing the block successfully.
The output shows the records. In this case, only three records are from Mumbai.
Advantages and Disadvantages of Cursor in Oracle
Given below are the advantages and disadvantages:
- Cursor fetches the data Row wise so Row Wise validation is possible using the cursor.
- It retrieves one row at a time, unlike SQL select query which selects all rows at a time.
- It is faster than While Loop but does have more overhead.
- It provides traversals over records of the database.
- They decrease the performance of inserts, updates, and deletes.
- They also take up space which depends on the number of fields used.
In the article, we discussed what a cursor really is in the introduction part and then the different types of cursors along with their attributes and syntax. We got to see the working of the cursor through example and as well as the advantages and disadvantages of using the cursor.
This has been a guide to Cursor in Oracle. Here we discuss the introduction, cursor types, advantages, disadvantages, and example of cursor in oracle. You may also have a look at the following articles to learn more –