Introduction
This article is all about cursors in SQL Server.
Cursors
In order to work with a cursor we need to perform some steps in the following order
Types of Cursors
Base Table Cursors
Static Cursors
Forward-only Cursors
Keyset-driven Cursors
FETCH command also having various types:
E.g.: - For Forward-only Cursors DECLARE @Complaint_Id Int DECLARE Merge_Cursor CURSOR FAST_FORWARD FOR Select Cust_CMP_Id from CR_Complaint_Master
Open Merge_Cursor FETCH NEXT FROM Merge_Cursor INTO @Complaint_Id WHILE @@FETCH_STATUS = 0 BEGIN
UPDATE CR_Complaint_Master SET Cust_CMP_State= 'Karnataka' WHERE Cust_CMP_Id = @Complaint_Id FETCH NEXT FROM Merge_Cursor INTO @Complaint_Id END
CLOSE Merge_Cursor DEALLOCATE Merge_Cursor Advantages of Cursor:
Used to update the values row by row.
Disadvantages of Cursor:
Mostly avoid the cursors. Because it reduce the performance and scalability of your applications.
Here are some alternatives to using a cursor:
Use WHILE LOOPS
Use temp tables
Use derived tables
Use correlated sub-queries
Use the CASE statement
Perform multiple queries
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: