Using transaction on a single update statement

前端 未结 3 786
执念已碎
执念已碎 2021-02-07 13:09

I am dubbing some SP at work and I have discover that whoever wrote the code used a transaction on a single update statement like this

begin transaction 
*single         


        
3条回答
  •  春和景丽
    2021-02-07 13:16

    I understand that this is wrong because transaction is used when you want to update multiple tables.

    Not necessarily. This involves one table only - and just 2 rows:

    --- transaction  begin
    
    BEGIN TRANSACTION ;
    
    UPDATE tableX 
    SET Balance = Balance + 100
    WHERE id = 42 ;
    
    UPDATE tableX 
    SET Balance = Balance - 100
    WHERE id = 73 ;
    
    COMMIT TRANSACTION ;
    
    --- transaction  end
    

提交回复
热议问题