SQL transaction & error handling template
Here is a small template to execute a SQL statement in a transaction scope, with a rollback if an error occured.
BEGIN TRAN
/*
( SQL CODE TO EXECUTE HERE)
*/
GO
IF (@@ERROR = 0)
BEGIN
COMMIT
PRINT 'EXECUTION COMPLETED WITH SUCCESS'
END
ELSE
BEGIN
ROLLBACK
PRINT 'ERROR HAS OCCURED'
END
No comments:
Post a Comment