Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My Try / Catch / Begin Tran / Rollback tran question 1

Status
Not open for further replies.

djj55

Programmer
Feb 6, 2006
1,761
US
Hello, SQL2008R2
Question one if you execute a TRUNCATE TABLE can this be rolled back?

In a Try/Catch if I include Raiseerror then do a Rollback with the error still work?
Code:
TRY CATCH
    BEGIN TRANSACTION My1;
    TRUNCATE TABLE MyTable;
    .
    .  My code here
    .
    
    COMMIT TRANSACTION My1;
END TRY
BEGIN CATCH
    DECLARE @ErrorMessage NVARCHAR(4000);
    DECLARE @ErrorSeverity INT;
    DECLARE @ErrorState INT;

    SELECT 
        @ErrorMessage = ERROR_MESSAGE(),
        @ErrorSeverity = ERROR_SEVERITY(),
        @ErrorState = ERROR_STATE();

    RAISERROR (@ErrorMessage, -- Message text.
               @ErrorSeverity, -- Severity.
               @ErrorState -- State.
               );

    ROLLBACK TRANSACTION My1;

END CATCH
Notice that the raiserror code is directly from BOL.
Thanks

djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!
 
You can rollback a truncate statement. See here:
I'm not 100% sure, but I think you may need to rollback the transaction before you raiserror. You should probably test this.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thank you for the reply. It is early and I should have seen the obvious, move the rollback. Doh!

Next question. Will Raising an error in a catch also do a return? This is what I would expect.

Thank you again,



djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!
 
I just wanted to say thanks for posting this question. This and BEGIN TRAN statements in general are 2 things that I'm trying to get myself accustomed to using - or at least thinking of times of when I should be using them.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Your welcome. As you can see I am learning. Most of our code was for SQL2000 so when we went to SQL2008R2 and I can use Try/Catch I do not think to use it unless there is a problem.

djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!
 
I would very strong looking up and doing some reading around XACT_ABORT() and making use of XACT_STATE() in your catch block to determine the validity of rolling back a transaction as it is possible for the rollback itself to throw and error if it is determined that there is no transaction.

Rhys

"Technological progress is like an axe in the hands of a pathological criminal"
"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
Albert Einstein
 
Rhys, thank you for the warning.


djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!
 
I use SQL Complete, and its template does it this way (with your code inserted):

Code:
BEGIN TRY
	BEGIN TRANSACTION My1;
		TRUNCATE TABLE MyTable;
	--.
	--.  My code here
	--.
	COMMIT TRANSACTION My1;
END TRY
BEGIN CATCH
	PRINT 'Error ' + CONVERT(VARCHAR(50), ERROR_NUMBER()) +
	', Severity ' + CONVERT(VARCHAR(5), ERROR_SEVERITY()) +
	', State ' + CONVERT(VARCHAR(5), ERROR_STATE()) +
	', Line ' + CONVERT(VARCHAR(5), ERROR_LINE())
	PRINT ERROR_MESSAGE();

	IF XACT_STATE() <> 0
		BEGIN
			ROLLBACK TRANSACTION My1;
		END

END CATCH;

Note that this doesn't do a RAISERROR at all.




-- Francis
Francisus ego, sed non sum papa.
 
Thank you, Francis

I need the raiserror as we have a message log file that a failure notice was pushed to, but we did not know why as the SQL Job finish successfully.

I guess I could push the information to the message log, which would be similar to your method.

There are always several ways to do the same thing. :)

djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!
 
djj55 said:
I guess I could push the information to the message log, which would be similar to your method.

There is an example of that here.

-- Francis
Francisus ego, sed non sum papa.
 
I would recommend you read this:
Basically, it shows an example of logging errors as well as how to construct the code with the error processing included.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Excellent, George. Thanks.

-- Francis
Francisus ego, sed non sum papa.
 
Please note, (based on my earlier post and the code in the provided link), although both the XACT_STATE and @@TRANCOUNT functions can be used to detect whether the current request has an active user transaction @@TRANCOUNT cannot be used to determine whether that transaction has been classified as uncommittable and XACT_STATE cannot be used to determine whether there are nested transactions.

Rhys

"Technological progress is like an axe in the hands of a pathological criminal"
"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top