 |
| SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 11th, 2004, 04:52 AM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Another Transaction question
Say you have the following:
Code:
BEGIN TRANSACTION
--do some work
--do some more work
--etc
COMMIT
If there is a problem during the work done, will SQL Server automatically perform a ROLLBACK on this transaction? Or is it my responsibility to check for errors and then rollback?
Thanks in advance.
Gert
|
|

March 11th, 2004, 07:41 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
|
|
If you explicitly begin a transaction, then you are responsible for explicitly committing it and/or explicitly rolling it back. You must check @@ERROR after each statement and act accordingly.
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
|
|

March 11th, 2004, 07:43 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Gert,
Here, it is your responsibility to check for errors and Rollback.
You can do that this way.
BEGIN TRANSACTION
--do some work
If @@ERROR<>0
ROLLBACK TRAN
RETURN
END
COMMIT TRAN
Cheers,
-Vijay G
|
|

March 11th, 2004, 04:23 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks to both of you.
But I wonder, what will happen if the transaction is written like in my original posting? Will the work be committed even if an error occurs?
Gert
|
|

March 11th, 2004, 06:19 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Quote:
quote:
But I wonder, what will happen if the transaction is written like in my original posting? Will the work be committed even if an error occurs?
|
Yup, assuming that no fatal error occurs which causes the entire stored procedure to abort.
Since you took no action on any error by testing @@ERROR, the code will run as written, happily falling through to your COMMIT...
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
|
|

March 12th, 2004, 12:46 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
|
|
I neglected to point out that there is a SET option which you can use to control this. See BOL for:
SET XACT_ABORT ON
Sorry for the oversight.
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
|
|

March 12th, 2004, 01:52 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Jeff. This was more to my liking.. :)
|
|

March 16th, 2004, 01:20 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by Jeff Mason
Quote:
|
But I wonder, what will happen if the transaction is written like in my original posting? Will the work be committed even if an error occurs?
|
Yup, assuming that no fatal error occurs which causes the entire stored procedure to abort.
Since you took no action on any error by testing @@ERROR, the code will run as written, happily falling through to your COMMIT...
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
|
and if a fatal error ocurs? will the transactions be rolld back then? in BOL it says that the process is suspended and the connection is broken, but nothing about transactions...
thanks,
defiant.
|
|

March 16th, 2004, 02:44 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
|
|
If a severe/fatal error occurs which will cause the SP to abort, the transaction is rolled back. If the connection is broken because of e.g. network errors, the transaction is rolled back. If the error is not severe (syntax error in a query, constraint errors, etc. for example) such that only the currently executing statement is aborted, the stored procedure will continue to execute and transaction COMMIT/ROLLBACKs will be executed as they are encountered. In this case it is the responsibility of the programmer to test @@ERROR and act accordingly.
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
|
|
 |