简体   繁体   English

尽管我有“提交交易”声明,但交易未提交

[英]Transactions not being committed although I have “Commit Transaction” statement

I'm using SQL Azure and trying to do conditional delete in batches for a large table, sample:我正在使用 SQL Azure 并尝试对大表进行批量条件删除,示例:

DECLARE
    @LargestKeyProcessed BIGINT =1,
    @NextBatchMax BIGINT,
    @msg varchar(max) ='';
 
WHILE (@LargestKeyProcessed <= 1000000)
BEGIN
 Begin Transaction
 SET @NextBatchMax = @LargestKeyProcessed + 50000;
  DELETE From mytable
  WHERE Id > @LargestKeyProcessed AND Id <= @NextBatchMax And some logic
  SET @LargestKeyProcessed = @NextBatchMax;
    set @msg=''+@LargestKeyProcessed;
  RAISERROR(@msg, 0, 1) WITH NOWAIT
  Commit Transaction
END

After the command gets executed successfully I close the tab but SSMS says there are uncomitted transactions although the commit statement is in every iteration.命令成功执行后,我关闭选项卡,但 SSMS 说有未提交的事务,尽管提交语句在每次迭代中。 Also the database size seems to remain the same.此外,数据库大小似乎保持不变。

I kindly seek your support in explaining why this happens我恳请您的支持来解释为什么会发生这种情况

Thank you very much非常感谢

I think you can try to add SET IMPLICIT_TRANSACTIONS OFF to the sql.我认为您可以尝试将SET IMPLICIT_TRANSACTIONS OFF添加到 sql。 As follows to see if solved your issue.如下,看看是否解决了您的问题。

DECLARE
    @LargestKeyProcessed BIGINT =1,
    @NextBatchMax BIGINT,
    @msg varchar(max) ='';
 
WHILE (@LargestKeyProcessed <= 1000000)
BEGIN
 SET IMPLICIT_TRANSACTIONS OFF  
 Begin Transaction
 SET @NextBatchMax = @LargestKeyProcessed + 50000;
  DELETE From mytable
  WHERE Id > @LargestKeyProcessed AND Id <= @NextBatchMax And some logic
  SET @LargestKeyProcessed = @NextBatchMax;
    set @msg=''+@LargestKeyProcessed;
  RAISERROR(@msg, 0, 1) WITH NOWAIT
  Commit Transaction
END

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

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