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)
 my transaction isnt rolling back when it should

Author  Topic 

BitShift
Yak Posting Veteran

98 Posts

Posted - 2008-04-09 : 18:47:25
Ive got an insert statement that fails, and below that I have code like the following:


IF @@ERROR <> 0
BEGIN
-- Roll back the transaction
ROLLBACK
-- Raise an error and return
RAISERROR ('Error INSERT INTO Address.', 16, 1)
print 'test was here'
RETURN
END


However, there is now rollback and the inserts below it are going through.

what do i have wrong ?

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-04-09 : 19:10:54
Are there any statements between the "INSERT" and the "IF @@ERROR" ?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-09 : 19:17:12
Is it in a stored procedure? Could you post the whole thing so that we can make sure it is correct?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

BitShift
Yak Posting Veteran

98 Posts

Posted - 2008-04-10 : 09:22:38
quote:
Originally posted by Lamprey

Are there any statements between the "INSERT" and the "IF @@ERROR" ?



Yes, and apparently thats what my problem was.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-10 : 09:29:50
@@ERROR is highly volatile. It is set to new value after every statment.
And even referring to @@ERROR sets @@ERROR to 0.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-10 : 09:31:06
You can do a "workaround".

INSERT ..

SET @MyErrorVariable = @@ERROR

... More code here ...

IF @MyErrorVariable <> 0
ROLLBACK



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -