Cursor in SQL Server
Cursor in SQL Server
Introduction
• A cursor is a database object that allows us to retrieve each row at a
time and manipulate its data.
• Syntax
• Syntax
OPEN cursor_name;
Fetch Cursor Statement
• In this step the cursor should be closed after we finished work with a
cursor. We can do this by using the below SQL statement:
CLOSE cursor_name;
types of Cursors:
1. Implicit Cursors,
2. Explicit Cursors.
1.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.
2.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.
Implicit Cursors Explicit Cursors
Implicit cursors are automatically created when Explicit cursors needs to be defined explicitly by the
select statements are executed. user by providing a name.
They are capable of fetching a single row at a time. Explicit cursors can fetch multiple rows.
Provides less programmatic control to the users User/Programmer has the entire control.
Implicit Cursors are defined as: Explicit cursors are defined as:
BEGIN SELECT attr_name from table_name where DECLARE CURSOR cur_name IS SELECT attr_name
CONDITION; END from table_name where CONDITION; BEGIN ...