在存储过程中设置显式事务的正确方法
我创建了一个存储过程,在汇总表中有 9 次插入,最后一条语句是删除。
对于每个给定日期,执行此过程大约需要 9 分钟,我注意到的一件事是,如果发生错误并且执行了前 3 个插入,它会保留插入的数据。为了处理这个问题,我创建了一个开始尝试并开始事务,我所做的测试是使用 SSMS 启动该过程,启动后,我取消了该命令,但保留了该事务。我怎样才能避免这种情况?
程序主体非常简单......类似于:
insert into...
insert into...
insert into...
insert into...
insert into...
insert into...
insert into...
insert into...
insert into...
insert into...
delete from....
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两件事:
1) 基本语法是:
2) 如果取消正在进行的批处理,则可能需要手动运行 ROLLBACK TRAN。您可以通过执行 SELECT @@TRANCOUNT 来测试是否需要执行此操作,以及它是否 > > 0 然后调用 ROLLBACK TRAN。
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.