Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 @@ROWCOUNT issue

Author  Topic 

ekareem
Starting Member

9 Posts

Posted - 2006-03-31 : 16:57:55
Hi,
I ran the following code and got @rows to display the value 1 - I guess it should display 0 because the IF statement returns no rows of course - So why is it displaying 1?

DECLARE @ERRORS INT
SELECT 5,10,15
IF @@ERROR > 0
SET @ERRORS=1
ELSE
SET @ERRORS=0
SELECT 'ROW_Count', @@ROWCOUNT --> shows 1 !!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-03-31 : 17:15:59
It returns 1 because the statement SET @ERRORS=0 returns 1 row. You'll want to put the value of @@ERROR and @@ROWCOUNT into variables all in one statement to avoid this issue.

DECLARE @e int, @rc int

...

SELECT @e = @@ERROR, @rc = @@ROWCOUNT

Tara Kizer
aka tduggan
Go to Top of Page

ekareem
Starting Member

9 Posts

Posted - 2006-04-02 : 01:21:42
Very clever. Thanks
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-04-03 : 01:18:58
Read more about Error handling
http://www.sommarskog.se/error-handling-I.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -