Curaor in SQL

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

What is Cursor in SQL ?

Cursor is a Temporary Memory or Temporary Work Station. It is Allocated by Database Server at the Time of
Performing DML(Data Manipulation Language) operations on Table by User. Cursors are used to store Database
Tables. There are 2 types of Cursors: Implicit Cursors, and Explicit Cursors. These are explained as following below.

Implicit Cursors:
Implicit Cursors are also known as Default Cursors of SQL SERVER. These Cursors are allocated by SQL
SERVER when the user performs DML operations.
Implicit cursors are cursors that are created and managed automatically by the database management
system (DBMS) in response to certain SQL statements. These cursors are not visible to the user and are
used to manage the data returned by a SELECT statement. Implicit cursors are typically used in situations
where only a single row is returned by the query and no further manipulation of the data is required.
Implicit cursors are typically used for simple operations that only affect a single row of data and do not
require any additional processing or manipulation.

Explicit Cursors :
Explicit Cursors are Created by Users whenever the user requires them. Explicit Cursors are used for
Fetching data from Table in Row-By-Row Manner.
Explicit cursors are created and managed by the user through the use of the DECLARE CURSOR statement
in SQL.Explicit cursors are typically used when a query returns multiple rows of data. They allow the user to
fetch data from a table in a row-by-row manner, and provide more control over the data being returned
than implicit cursors. Explicit cursors also allow the user to perform operations such as scrolling, updating,
and deleting specific rows of data. They are useful when you need to perform complex data manipulation
or when you want to perform the same operation on multiple rows at once.
It is important to remember that using explicit cursors can be use more resource than using implicit
cursors because the DBMS has to maintain the additional state information associated with the cursor.
We can describe the life cycle of a cursor into the five different sections as follows:
Declaring the Cursor
Declaring the cursor defines the cursor with a name and the associated SELECT statement. For example –

Opening the Cursor


Opening the cursor allocates the memory for the cursor and makes it ready for fetching the rows returned
by the SQL statement into it. For example, we will open the above defined cursor as follows –

Fetching the Cursor


Fetching the cursor involves accessing one row at a time. For example, we will fetch rows from the above-
opened cursor as follows –

Closing the Cursor

Closing the cursor means releasing the allocated memory. For example, we will close the above-opened
cursor as follows –
Limitations of SQL Server Cursor

 Cursor consumes network resources by requiring a network roundtrip each time it fetches a record.
 A cursor is a memory resident set of pointers, which means it takes some memory that other
processes could use on our machine.
 It imposes locks on a portion of the table or the entire table when processing data.
 The cursor’s performance and speed are slower because they update table records one row at a
time.
 Cursors are quicker than while loops, but they do have more overhead.
 The number of rows and columns brought into the cursor is another aspect that affects cursor
speed. It refers to how much time it takes to open your cursor and execute a fetch statement
 It’s not recommended to use cursor for large data set, it’s better to use set-based approach.
 If you need to move backward you need to refetch the data again.

Attributes & Description Of Cursor

You might also like