Deadlock at trigger


a trigger need insert or update record @ other table in high traffic environments
however, deadlock happens; ie, trigger running twice @ same time want need insert record @ table.

i trid change isolation serializable or repeatable , cannot solve problem. guess "while loop" affects result because try cancel loop , execute smoothly.

? how solve deadlock???
thx

alex

-----------------------------
i used following commands change isolation level:
set transaction isolation level serializable
begin transaction
commit transaction

-----------------------------
the code follows:
     declare setskucursor cursor
     local
     static
     for select item, quantity invset sku = @sku
     open setskucursor
     fetch next setskucursor @invsetitem, @invsetqty
     while @@fetch_status = 0
      begin

       select @balsku = sku, @opendate = opendate invbal sku = @invsetitem
       if (@balsku not null)
        begin               
         if @txdate <= @opendate
          update invbal set slsqtynow = slsqtynow + @itmtxqty * @invsetqty sku = @invsetitem
         if @txdate > @opendate
          update invbal set slsqtynxt = slsqtynxt + @itmtxqty * @invsetqty sku = @invsetitem
        end
       else
        begin
       
         insert invbal (shop, sku, opendate, slsqtynow) values (@shop, @invsetitem, @bizdate, @itmtxqty * @invsetqty)
        
        end
       fetch next setskucursor @invsetitem, @invsetqty
      end
     close setskucursor
     deallocate setskucursor
-------------------------------------------------------------------

i tried use temp table receive insert sql statement , use stored procedure replace cursor loop. it's solved.


SQL Server  >  SQL Server Database Engine



Comments

Popular posts from this blog

Motherboard replacement

Cannot create Full Text Search catalog after upgrading to V12 - Database is not fully started up or it is not in an ONLINE state

Remote Desktop App - Error 0x207 or 0x607