I created a stored procedure that has 9 inserts in summarized tables and the last statement is a delete.
This procedure takes around 9 minutes to execute for each given date, and one thing that I noticed is that if an error occurs and the first 3 inserts executed, it keeps the inserted data. To handle this, I created a begin try and begin transaction and a test that I did was to start the procedure using SSMS and after it started, I cancelled the command but the transaction was kept. How can I avoid this?
the procedure body is very simple… something like:
insert into...
insert into...
insert into...
insert into...
insert into...
insert into...
insert into...
insert into...
insert into...
insert into...
delete from....
thanks
Two things:
1) The basic syntax is:
2) If you cancel a batch in process, you might need to run the ROLLBACK TRAN manually. You can test if you need to do this by doing a SELECT @@TRANCOUNT and if it is > 0 then call ROLLBACK TRAN.