CREATE procedure [dbo].[insert_SP]ASDECLARE @errorcode intBEGIN TRAN t1INSERT INTO TaBLE1 VALUES(2)INSERT INTO TaBLE2 VALUES('A')SELECT @errorcode = @@ERRORPRINT @errorcodeIF(@errorcode<>0)BEGINGOTO PROBLEMENDINSERT INTO TaBLE3 VALUES(3)COMMIT TRAN t1PROBLEM: IF(@errorcode<>0)BEGINPRINT 'Unexpected error occured'ROLLBACK TRAN t1ENDGO
create table dbo.table1(aCol int);create table dbo.table2(aCol int);create table dbo.table3(aCol int);goCREATE procedure [dbo].[insert_SP]ASBEGINDECLARE @errorcode intBEGIN TRAN t1begin try INSERT INTO dbo.table1 VALUES(2) INSERT INTO dbo.table2 VALUES('A') INSERT INTO dbo.table3 VALUES(3) COMMIT TRAN t1end trybegin catch PRINT 'Unexpected error occured' ROLLBACK TRAN t1end catchENDGOexec dbo.insert_SP;godrop procedure dbo.insert_SP;godrop table dbo.table1;drop table dbo.table2;drop table dbo.table3;go