[英]Transaction in Stored procedure
Please can someone help. 请有人帮忙。
I have several stored procedures in a job, and In one of them I Begin a transaction to delete some rows and if rows are greater than 10 then I Roll back. 我在一个作业中有几个存储过程,在其中一个过程中,我开始一个事务以删除一些行,如果行大于10,则回滚。 however if there are not I don't want to commit straight away, because 2 stored procedure later I do something similar.
但是,如果没有,我不希望立即提交,因为2存储过程以后我会做类似的事情。 however if count is greater than 10 in this instance I want it rolled back all the way to when I stared the transaction (two stored procedures ago)
但是,如果在这种情况下count大于10,我希望它一直回滚到我启动事务时的状态(两个存储过程之前)
Is it possible to start a transaction in a store procedure and have multiple roll backs and Commit right at the end somewhere or do I have to put all the code into 1 store procedure to do that? 是否可以在存储过程中启动事务并具有多个回滚并在某个地方的结尾处立即提交,还是必须将所有代码放入1个存储过程中才能做到这一点?
You can use @@TRANCOUNT to determine whether or not you have any outstanding uncommitted transactions and then use that to dictate the logic of your stored procedures. 您可以使用@@ TRANCOUNT来确定您是否有未完成的未提交事务,然后使用它来指示存储过程的逻辑。
CREATE PROCEDURE Proc1
AS
BEGIN
BEGIN TRANSACTION
// DO STUFF
IF (@@ROWCOUNT > 10)
ROLLBACK TRANSACTION
END
CREATE PROCEDURE Proc2
AS
BEGIN
IF (@@TRANCOUNT > 0)
BEGIN
// DO STUFF
IF (@@ROWCOUNT > 10)
ROLLBACK TRANSACTION
END
END
CREATE PROCEDURE Proc3
AS
BEGIN
IF (@@TRANCOUNT > 0)
BEGIN
// DO STUFF
IF (@@ROWCOUNT > 10)
ROLLBACK TRANSACTION
ELSE
COMMIT TRANSACTION
END
END
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.