Home > SQL Server > SQL SERVER – Cursor System Functions @@CURSOR_ROWS

SQL SERVER – Cursor System Functions @@CURSOR_ROWS


@@CURSOR_ROWS is the cursor system functions in SQL Server that is used to check the current cursor row count. This is useful when we are working with one or more cursors in the procedures. We have to remember that this function is a non-deterministic. So, we have use these functions with extra logic to make right decision for the program flow.

@@CURSOR_ROWS

This function returns the row count or current status of the last opened cursor object. It returns an integer value for the row count or other value as given in the below image.

pic0Syntax

@@CURSOR_ROWS

Example

1. This example returns -1 for the Cursor_Rows output. Because, the select * statement returns different rows based on the table transaction.

pic12. This example returns the actual row count value because the row count is static in this query.

pic2

Original Script for your experiment:

DECLARE trans CURSOR FOR
SELECT TOP 10 [Ac_No],[Transaction_Type] FROM [tblTransaction];

OPEN trans;
FETCH NEXT FROM trans;
SELECT @@CURSOR_ROWS AS 'Total Rows/Status';

CLOSE trans;
DEALLOCATE trans;
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: