SQL Server Cursor Operators – Part1

Hi Friends,

Today we are going to start on SQL Server cursor operators and continue our discussion for next few days. Let us consider following CURSOR which is declared with no options then traversed through using FETCH NEXT returning list of TerritoryName satisfying WHERE clause.

--Click on Display Estimated Execution Plan icon
--Database used in example is [AdventureWorks2012]

DECLARE TerritoryName CURSOR FOR

SELECT SalesTerritory.Name FROM [Sales].[SalesTerritory]
WHERE SalesTerritory.[Group] LIKE '%America%'

OPEN TerritoryName
FETCH NEXT FROM TerritoryName

WHILE @@FETCH_STATUS = 0 
BEGIN
	FETCH NEXT FROM TerritoryName
END

CLOSE TerritoryName
DEALLOCATE TerritoryName

QueryPlan_1

QueryPlan_2

QueryPlan_3

Note : Execution plan is divided into three parts in order to fit into the window.

We will split our discussion into multiple parts and today let us talk about following section of the CURSOR.

DECLARE TerritoryName CURSOR FOR

SELECT SalesTerritory.Name FROM [Sales].[SalesTerritory]
WHERE SalesTerritory.[Group] LIKE '%America%'

This part of the query produce Part 1 (see above image) of the execution plan. This definition includes SELECT statement, which provides the data cursor uses. First we have a Clustered Index Scan against SalesTerriroty table. Followed by a Compute Scalar operation (read more here Part1, Part2) which creates a unique identifier to identify data returned by the query.

   

CIScanToolTipCSToolTip

With new key values these rows are then inserted into a temporary clustered index which is created tempdb. With the help of this table and clustered index SQL Server is able to navigate through set of data in cursor, this table if often referred as a work table.

CIInsertToolTip

After this, we have Fetch Query operation which is the one that actually retrieves the rows from a cursor. ToolTip of this operator shown below.

FetctToolTip

At the end, we see Dynamic operator. This operator contains the definition of the cursor itself. In our example, cursor is dynamic which means it see the data changes made by others including inserts. The ToolTip of this operator is as following;

DynamicToolTip

It displays both DECLARE as well as SELECT statement. Stopping here, we are going to continue on the next parts tomorrow and day after.

Happy Learning!

Regards,

Kanchan

Like us on FaceBook | Join the fastest growing SQL Server group on FaceBook | Follow me on Twitter | Follow me on FaceBook

   

About Kanchan Bhattacharyya

Kanchan is an astute IT professional, a seasoned SQL Database Administrator with 13+ years of industry experience. A calculated risk taker with deep technical knowledge and has gained proficiency in database consulting and solutions across different environments. Kanchan holds MBA degree in IT and an International Executive MBA in Project Management. He is very passionate about SQL Server and holds MCSE (Data Platform), MCSA – SQL 2012, MCITP – SQL 2008 certifications. Currently he is focusing on cloud and latest releases of SQL Server. When not working, Kanchan likes to spend his time reading on new technical developments specifically on SQL Server and other related technologies.

View all posts by Kanchan Bhattacharyya →

Leave a Reply

Your email address will not be published.