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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Best way for Atomic testing in SP

Author  Topic 

dee-u
Starting Member

14 Posts

Posted - 2009-02-25 : 12:27:15
This is how I am currently doing it, I am checking both @@ERROR and @@ROWCOUNT to determine if an action query failed or not. Is this the best and most reliable way of doing such?
DECLARE @errorvar1 int, @rowcountvar1 int
DECLARE @errorvar2 int, @rowcountvar2 int
DECLARE @errorvar3 int, @rowcountvar3 int
DECLARE @errorvar4 int, @rowcountvar4 int

--Start transaction
BEGIN TRANSACTION
INSERT INTO ...
--Log error and rows affected
SELECT @errorvar1 = @@ERROR, @rowcountvar1 = @@ROWCOUNT

UPDATE Table ...
--Log error and rows affected
SELECT @errorvar2 = @@ERROR, @rowcountvar2 = @@ROWCOUNT

INSERT INTO ...
--Log error and rows affected
SELECT @errorvar3 = @@ERROR, @rowcountvar3 = @@ROWCOUNT

UPDATE TABLE ...
--Log error and rows affected
SELECT @errorvar4 = @@ERROR, @rowcountvar4 = @@ROWCOUNT

IF
@errorvar1 = 0 AND @rowcountvar1 >= 1 AND
@errorvar2 = 0 AND @rowcountvar2 >= 1 AND
@errorvar3 = 0 AND @rowcountvar3 >= 1 AND
@errorvar4 = 0 AND @rowcountvar4 >= 1
BEGIN
COMMIT TRANSACTION
RETURN 1
End
Else
BEGIN
ROLLBACK TRANSACTION
RETURN -1
End

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-25 : 12:38:20
http://www.sommarskog.se/error-handling-II.html
Go to Top of Page

dee-u
Starting Member

14 Posts

Posted - 2009-02-25 : 12:53:42
Thanks a lot sodeep, that was a handy link. But still it does not say if checking for both @@ROWCOUNT and @@ERROR should be done, what if both @@ERROR and @@ROWCOUNT is 0? It means there was no error and yet there was not any row affected?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-02-25 : 13:18:25
It's not something that we can answer. Your business requirements should dictate this.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dee-u
Starting Member

14 Posts

Posted - 2009-02-27 : 02:28:27
Ok, thank you guys (or gals).

I am thinking, instead of returning -1 for unsuccessful atomic processing of my action queries I should instead use RAISERROR, will there be repercussions on raising errors to tell that something has not succeeded?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-02-27 : 12:13:28
That's fine.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -