Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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
 
Old March 11th, 2004, 04:52 AM
Authorized User
 
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old March 11th, 2004, 07:41 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old March 11th, 2004, 07:43 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old March 11th, 2004, 04:23 PM
Authorized User
 
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old March 11th, 2004, 06:19 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old March 12th, 2004, 12:46 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old March 12th, 2004, 01:52 PM
Authorized User
 
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Jeff. This was more to my liking.. :)
 
Old March 16th, 2004, 01:20 PM
Authorized User
 
Join Date: Jun 2003
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Jeff Mason
 
Quote:
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
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.
 
Old March 16th, 2004, 02:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
COM+ transaction object hoetat VB How-To 3 May 29th, 2006 11:48 PM
Transaction Logs kilika SQL Server 2000 17 September 30th, 2004 09:26 AM
use of transaction log madhukp SQL Server 2000 1 July 16th, 2004 06:20 PM
Transaction Management sonaidg Java Databases 2 August 19th, 2003 04:20 AM
how to use Transaction ? locka ADO.NET 0 August 12th, 2003 08:48 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.