Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello There

There is a confusion between @@ROWCOUNT and @@CURSOR_ROWS.
I want to fetch data with Select query and stored it into the CURSOR as:-

SQL
DECLARE CUR_Invoice CURSOR FOR
SELECT * FROM  Invoice
OPEN CUR_Invoice
FETCH NEXT FROM CUR_Invoice INTO @SUB_CODE,@VOU_NO,@VOU_DATE,@AMOUNT...


Now I want to check if the records are fetched with the above query.
For this, should i use either @@ROWCOUNT or @@CURSOR_ROWS.
E.g.:-

SQL
IF(@@CURSOR_ROWS>0)
BEGIN
   PRINT 'RECORDS AVAILABLE';
END
CLOSE CUR_Invoice
DEALLOCATE CUR_Invoice


---
Need Suggestion
Rgds
Posted
Updated 27-Jul-13 19:27pm
v2

1 solution

Usually you use @@FETCH_STATUS.

See this MSDN Article[^] for example.

BTW, if the only reason for this cursor to check if there are records available in the table you could use EXISTS:

SQL
IF EXISTS(SELECT 1 FROM Invoice)
BEGIN
   PRINT 'RECORDS AVAILABLE';
END
 
Share this answer
 
v2
Comments
Mayank Topiwala 25-Jun-13 0:53am    
Thanx Andrew. Plz Check if below statement is valid for n no. of rows:

Declare @countValue INT
Declare CUR_Invoice CURSOR FOR
Select * From Invoice
OPEN CUR_Invoice
FETCH NEXT FROM CUR_Invoice INTO @......
While(@@FETCH STATUS = 0)
BEGIN
Set @countValue = @@ROWCOUNT;
BREAK;
FETCH NEXT FROM CUR_Invoice INTO @......
END
CLOSE ...
DEALLOCATE...
chaau 25-Jun-13 1:06am    
I do not understand what is the purpose of these two lines:
Set @countValue = @@ROWCOUNT;
BREAK;
What are you trying to count?
Mayank Topiwala 25-Jun-13 1:10am    
The No. of Row Fetched by Select query
chaau 25-Jun-13 1:20am    
If you just need the number of rows use this:
Set @countValue = (SELECT COUNT(*) FROM Invoice)

If you really need to do this via the cursor use this:
Declare @countValue INT
Declare CUR_Invoice CURSOR FOR Select * From Invoice
OPEN CUR_Invoice
Set @countValue = 0;
FETCH NEXT FROM CUR_Invoice INTO @......
While(@@FETCH STATUS = 0)
BEGIN
Set @countValue = @countValue + 1;
FETCH NEXT FROM CUR_Invoice INTO @......
END
CLOSE ...
DEALLOCATE...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900