If I interpret your question, you are asking for a way to execute a number of statements within a stored procedure, and before exiting the proc, check if any errors occured during execution and then rollback all of the statements.
If that's a correct interpretation of your question, you should look into TRY/CATCH.
[http://msdn.microsoft.com/en-us/library/ms175976.aspx][1]
Here's an example of the usage:
CREATE PROC TryCatchExample
AS
BEGIN TRAN
--First statement
BEGIN TRY
INSERT INTO table1 (col1, col2) VALUES('value1','value2')
INSERT INTO table1 (col1, col2) VALUES('value1','value2')
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK
END CATCH
--If we didn't rollback, @@TRANCOUNT should be > 0 and we should commit
IF @@TRANCOUNT > 0
COMMIT
[1]: http://msdn.microsoft.com/en-us/library/ms175976.aspx
↧