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.

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.

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