What is cursor in Sql Server and its type?
In this article, I will explain what is meant by cursor and types of cursor with example codes and then components which are used in cursor with syntax and Example with Explanation.How the cursor access the data inside the sqlserver.
A Cursor is a set row together with a pointer to identify current row.Cursor is a Static cursor is a scrollable query cursor .Static cursors support scrolling backward and forward, but The forward-only cursor is the fastest cursor that you can update, but it does not support scrolling. Forward-only/Read-only is same as Forward-only . Its faster then Forward-only but can't be updated. The keyset-driven cursor is a scrollable cursor and then you can update.What is cursor in SQL Server?
Contents
Definition
Types of cursor
Components of Cursor
Syntax for Cursor
Example for CursorDefinition:
database object and manipulate data in row-by-row basics.Types of Cursor:
Base table
Static
Forward-only
Forward-only/Read-only
Keyset-drivenBase table:
Base table cursors are the lowest level of cursor .Base table cursors can scroll
forward or backward with minimal cost, and can be updated.Directly open the Cursor in Index.
Example:By using ADO.Net cmd.CommandText = "tablename";
cmd.CommandType = CommandType.TableDirect;
SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable); Static:
they do not support updates. Static cursors do not see external changes to the data that is insensitive.
Example:By using ADO.Net cmd.CommandText = "Select * from tablename";
SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Insensitive); Forward-only:
It supports only fetching the rows.Forward-only and forward-only/read-only cursors are the fastest query-based cursors.
Example:By using ADO.Net cmd.CommandText = "Select * from tablename";
SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Updatable); Forward-only/Read-only:
Example:By using ADO.Net cmd.CommandText = "Select * from tablename";
SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.None); Keyset-driven:
A keyset-driven cursor is controlled by a set of physical identifiers .A keyset-driven cursor is not as sensitive as other cursors.
Example:By using ADO.Net cmd.CommandText = "Select * from tablename";
SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable); Components for Cursor in Sql Server:
1)DECLARE statements :
Declare variables used in the code block .DECLARE @AuthorID char(11)
DECLARE c1 CURSOR
2)SET\SELECT statements:
Initialize the variables to a specific value .SELECT au_id
FROM authors
3)OPEN statement :
Open the cursor to begin data processing .OPEN c1
4)FETCH NEXT statements:
Assign the specific values from the cursor to the variables .FETCH NEXT FROM c1
INTO @AuthorID
5)WHILE statement :
Condition to begin and continue data processing .WHILE @@FETCH_STATUS = 0
6)Begin and End statemets:
Start and end of the code block .BEGIN
PRINT @AuthorID
FETCH NEXT FROM c1
INTO @AuthorID
END
7)Data processing :
In this example Print @AuthorIDPRINT @AuthorID
FETCH NEXT FROM c1
INTO @AuthorID
8)CLOSE statement:
Releases the current data , but permits the cursor to be re-opened .CLOSE c1
9)DEALLOCATE statement :
Destroys the cursor.DEALLOCATE c1
Cursor Syntax
DECLARE @AuthorID char(11)
DECLARE c1 CURSOR
FOR
SELECT au_id
FROM authors
OPEN c1
FETCH NEXT FROM c1
INTO @AuthorID
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @AuthorID
FETCH NEXT FROM c1
INTO @AuthorID
END
CLOSE c1
DEALLOCATE c1Example for Cursor:
DECLARE @Loan_details varchar(20)
DECLARE @getLoan_details CURSOR
SET @getLoan_details = CURSOR
FOR
SELECT Lnno
FROM test4
OPEN @getLoan_details
FETCH NEXT
FROM @getLoan_details INTO @Loan_details
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @Loan_details
FETCH NEXT
FROM @getLoan_details INTO @Loan_details
END
CLOSE @getLoan_details
DEALLOCATE @getLoan_details
In this example cursor fetch the loanno from loan_details by row -by-rowNote:
Cursors are the SLOWEST way to access data inside SQL Server.
And Cursor is thirty times slower than set based alternatives.
thanks ,,its great..