Cursor in SQL
In SQL, a cursor is a temporary workstation that is allocated by the database server during the execution of a statement.
It is a database object that allows us to access data of one row at a time. This concept of SQL is useful when the user wants to update the rows of the table one by one.
The cursor in SQL is the same as the looping technique of other programming languages. The collection of tuples held by the cursor is known as the active set.
In SQL database systems, users define the cursor using DECLARE statement and take the SELECT statement as the parameter, which helps in returning the set of rows.
In this SQL article, we will learn about the types of a cursor, the life cycle of a cursor, syntax of a cursor, and the implementation of a cursor.
Types of Cursor in SQL
Following are the two types of Cursor in Structured Query Language:
These types of cursors are generated and allocated by the SQL server when the system performs INSERT, DELETE, and UPDATE operations on SQL queries.
This cursor is also referred to as the default cursor in SQL.
An implicit cursor is also created by the system when the SELECT query selects the single row.
These types of cursors are created by the user using the SELECT query.
An explicit cursor holds multiple records but processes a single row at a time. It uses the pointer, which moves to another row after reading one row.
It is basically used for gaining extra control over the temporary workstation.
Life Cycle of Cursor
The life cycle of the cursor is described into the following five stages:
Let's discuss each stage in brief:
1. Declare a Cursor
First, we have to declare the cursor by using the following SQL syntax:
In this syntax, we have to specify the name and data type of the cursor just after the DECLARE keyword. After that, we have to write the SELECT statement, which defines the result set for the cursor.
2. Open Cursor
It is the second stage that opens the cursor for storing the data retrieved from the result set. We can open the cursor by using the following SQL syntax:
3. Fetch Cursor
It is the third stage in the cursor life cycle that fetches the rows for performing the insertion, deletion, and updation operations on the currently active tuple in the cursor.
Following are the six options that are used in syntax for fetching data from the cursor:
i. FIRST: This option allows the system to access only the first record from the cursor table. The syntax for the FIRST option is given below:
ii. LAST: This option allows the system to access only the last record from the cursor table. The syntax for the LAST option is as follows:
iii. NEXT: This method allows the system to access the data in the forward direction from the cursor table. It is the default option. The syntax of this method is as follows:
iv. PRIOR: This method allows the system to access the data in the backward direction from the cursor table. The syntax of this option is as follows:
v. ABSOLUTE n: This method allows the system to access the data of the exact nth row from the cursor table. The syntax of this option is mentioned below:
vi. RELATIVE n: This method allows the system to access the data in both incremental and decremental processes. The syntax of this option is mentioned below:
4. Close Cursor
It is the fourth stage in the process of the cursor. When we complete the work with the cursor, we have to close the cursor in this stage. We can close the cursor in SQL by using the following query:
5. Deallocate Cursor
It is the last and fifth stage of the cursor life cycle. In this part, we have to erase the definition of the cursor and discharge all the system resources combined with the cursor.
Syntax of a Cursor in SQL
Basic Type of Cursor
Following are the four basic types of cursor in Structured Query Language:
The static cursor can move in forward as well as backward direction. This type of cursor populates the result set during the creation of a cursor. Compared to other cursors, it is slow and uses more space in the memory.
By default, these types of cursors are scrollable. The static cursor does not allow the database users to modify and delete data.
Forward Only Cursor
This type of cursor accesses and updates the data only in the forward direction through the result set. So, it is the fastest cursor among all the four cursors.
The main disadvantage of this cursor is that it does not support backward scrolling.
Following are the three types of 'Forward only Cursor':
The dynamic cursor is just opposite to the static cursor. It allows us to execute the INSERT, DELETE, and UPDATE operations while the cursor is open.
It checks all the modifications done on the rows and values in the result set.
KEYSET Driven Cursor
This type of cursor accesses the data from the first to the last row and last to the first row. When the user opens the KEYSET cursor, it automatically creates the list of unique values which uniquely identify each row in the entire result set.
Example of Cursor in SQL
Create a Student table in SQL using the following query:
Now, insert some values into the above Student table as shown in the following block:
We can check the data of the Student table by using the following SELECT statement in SQL:
This query shows the data of the Student table in the output:
Now, we will create the following cursor for displaying the record of students from the Student table:
The above cursor gives the following output: