|
|
|
Savepoints offer a mechanism to roll back portions of transactions. A user can set a savepoint, or marker, within a transaction. The savepoint defines a location to which a transaction can return if part of the transaction is conditionally canceled. SQL Server allows you to use savepoints via the SAVE TRAN statement, which doesn't affect the @@TRANCOUNT value. A rollback to a savepoint (not a transaction) doesn't affect the value returned by @@TRANCOUNT, either. However, the rollback must explicitly name the savepoint: using ROLLBACK TRAN without a specific name will always roll back the entire transaction.
The following script demonstrates how savepoints can be used : |
Click here to copy the following block | USE pubs SELECT 'Before BEGIN TRAN main', @@TRANCOUNT
BEGIN TRAN main SELECT 'After BEGIN TRAN main', @@TRANCOUNT DELETE sales SAVE TRAN sales SELECT 'After SAVE TRAN sales', @@TRANCOUNT
BEGIN TRAN nested SELECT 'After BEGIN TRAN nested', @@TRANCOUNT DELETE titleauthor SAVE TRAN titleauthor SELECT 'After SAVE TRAN titleauthor', @@TRANCOUNT ROLLBACK TRAN sales
SELECT 'After ROLLBACK TRAN sales', @@TRANCOUNT
SELECT TOP 5 au_id FROM titleauthor
IF (@@TRANCOUNT > 0) BEGIN ROLLBACK TRAN SELECT 'AFTER ROLLBACK TRAN', @@TRANCOUNT END SELECT TOP 5 au_id FROM titleauthor |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|