简体   繁体   English

交易不提交

[英]Transaction not committing

In my SSMS I have checked the "SET IMPLICIT_TRANSACTIONS" because I do not want to do any commits automatically. 在我的SSMS中,我检查了“SET IMPLICIT_TRANSACTIONS”,因为我不想自动进行任何提交。 When I run the below code, the transaction still does not get committed. 当我运行以下代码时,事务仍然没有得到提交。 Am I missing something else? 我错过了别的什么吗?

BEGIN TRANSACTION
  BEGIN TRY
    EXECUTE prModifyOrAdd '73891821'
    COMMIT TRANSACTION
  END TRY
  BEGIN CATCH
    PRINT 'ERROR'
  END CATCH

Here is the design of the procedure: 这是程序的设计:

 CREATE PROCEDURE [dbo].[prModifyOrAdd] @id varchar(50) 
 as
 DECLARE @outMessage varchar(25)
 update INDV834
 set FILENAME= @id+'.txt'
 where SUBSCRIBER_ID=@id

 if (select distinct filename from INDV834 where SUBSCRIBER_ID=@id)=      (@id+'.txt')
   BEGIN
    SET @outMessage = 'Completed: ' + (@id+'.txt')
    select @outMessage
   END
 else
   BEGIN
    SET @outMessage = 'Errored'
    select @outMessage
   END

If you start a transaction explicitly, as you did, the set implict is ignored. 如果您显式启动事务,则会忽略set implict。 What is happening in the stored procedure? 存储过程中发生了什么? Does it have its own error handling and is it handling the error before you see it? 它是否有自己的错误处理,是否在您看到它之前处理错误?

Many users wrap their rollback with a @@trancount as so: 许多用户使用@@ trancount包装回滚,如下所示:

begin catch 开始捕捉

if @@trancount>0 if @@ trancount> 0

begin 开始

rollback

end 结束

end 结束

There are few reasons why you don't see the "data committed": 您没有看到“已提交数据”的原因很少:

0) If you have checked IMPLICT_TRANSACTION option in SSMS then after BEGIN TRANSACTION will be two active transactions. 0)如果您在SSMS中检查了IMPLICT_TRANSACTION选项,那么在BEGIN TRANSACTION将是两个活动事务。 This means that you need to COMMIT statements. 这意味着您需要COMMIT语句。

See also Nesting transactions 另请参见嵌套事务

Note: At this point ( IMPLICT_TRANSACTION ON + BEGIN TRANSACTION + just one COMMIT ), concurrent connections will be blocked because a transaction will be active ( SELECT @@TRANCOUNT ). 注意:此时( IMPLICT_TRANSACTION ON + BEGIN TRANSACTION + 只有一个 COMMIT ),并发连接将被阻止,因为事务将处于活动状态( SELECT @@TRANCOUNT )。 And, if you close SSMS window, current [implicit] transaction will be automatically cancelled (ROLLBACK) => you will loose any modifications made by stored procedure. 并且,如果关闭SSMS窗口,当前[隐式]事务将自动取消(ROLLBACK)=>您将丢失存储过程所做的任何修改。

My recommendation is to avoid IMPLICT_TRANSACTION . 我的建议是避免IMPLICT_TRANSACTION So, please uncheck this option. 所以,请取消选中此选项。

1) INSERT / UPDATE / DELETE / MERGE statements from dbo.prModifyOrAdd could afftect 0 rows. 1)来自dbo.prModifyOrAdd INSERT / UPDATE / DELETE / MERGE语句可能会占用0行。 For example, an UPDATE statement may have WHERE clause but conditions from WHERE are false , so 0 rows are updated. 例如, UPDATE语句可能具有WHERE子句,但WHERE中的条件为false ,因此更新了0行。

2) Target tables have INSTEAD OF triggers that do nothing or have some bugs . 2)目标表具有INSTEAD OF触发器,它们不执行任何操作或存在一些错误

Example: 例:

CREATE TABLE dbo.MyTable (ID INT IDENTITY(1,1) PRIMARY KEY, Col1 INT)
GO
INSERT dbo.MyTable VALUES (11)
INSERT dbo.MyTable VALUES (22)
GO

CREATE TRIGGER trgTrivialInsteadOfTrigger 
ON dbo.MyTable
INSTEAD OF UPDATE
AS
BEGIN
    PRINT 'trgTrivialInsteadOfTrigger activated'
END;
GO

UPDATE   dbo.MyTable
SET     Col1 = Col1 * 10;
SELECT * FROM dbo.MyTable;
GO
/*
-- Col1 isn't updated 11 -> 110 ...
ID          Col1
----------- -----------
1           11
2           22

(2 row(s) affected)
*/

3) You source code is executed in the context of another transaction that execute at the end ROLLBACK (see nesting transactions): 3)您的源代码在另一个在ROLLBACK结束时执行的事务的上下文中执行(请参阅嵌套事务):

BEGIN TRANSACTION

... your source code ...

ROLLBACK <-- this statement will cancel all modifications 

Example: 例:

DROP TRIGGER dbo.trgTrivialInsteadOfTrigger -- <-- see 2)
GO

BEGIN TRANSACTION
    BEGIN TRANSACTION 
        UPDATE   dbo.MyTable
        SET     Col1 = Col1 * 10;
    COMMIT
ROLLBACK
SELECT * FROM dbo.MyTable;
/*
ID          Col1
----------- -----------
1           11
2           22

(2 row(s) affected)
*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM