插入语句上的死锁

时间:2015-01-16 08:00:28

标签: sql-server stored-procedures deadlock

使用存储过程将listview中的一系列记录插入到我的SQL Server表中。但是我遇到了如下图所示的僵局

这是我的程序:

declare @debtorCollectorHistoryId bigint

set @debtorCollectorHistoryId = (Select top 1 id 
                                 from FVOfficer with(nolock) 
                                 where debtorid = @debtorid 
                                 order by id desc)

if @currentFvCollectorid  is null
   set @currentFvCollectorid = (SELECT FvCollectorID 
                                from Debtor with(nolock) 
                                where id = @debtorid)

if @assignon is null
   set @assignon = GETDATE()

      print @debtorCollectorHistoryId
       print @currentFvCollectorid
      print @newFvCollectorid



    if @currentFvCollectorid <> @newFvCollectorid
    BEGIN
        if @currentFvCollectorid <> 0
        Begin   
            update FVOfficer 
            set terminateon = GetDate()
            where id = @debtorCollectorHistoryId
        End

        Begin
        --create new Collector History for new collector
        --NOTE: CurrentCollectorID is a previous collectorID for new Collector History

        insert into FVOfficer (debtorid, prevCollectorID, collectorid, assignon, terminateon, loginID)
        values (@debtorid,@currentFvCollectorid, @newFvCollectorid, GetDate(), null, @loginID)
        print' create new Collector History for new FV collector'

        exec dbo.[ValidatePrevFVCollectorByDebtorID] @debtorid
    end
    begin
        print @debtorid
        update Debtor set FVCollectorid = @newFvCollectorid where id = @debtorid 
    end

从我的快照中,我假设在'FVOfficer'表上发生死锁,但我的currentFvCollectorid始终为0,因此不会执行更新语句。但是,我将@debtorid传递给过程[ValidatePrevCollectorByDebtorID],该过程使用不同的表而非FVOfficer表上的游标执行某些验证。可能是参数@debtorid被第二个过程锁定并且插入中需要它吗?如果是这样,请我如何解决这个问题。

当我检查我的表FVOfficer时,我注意到它没有主键或任何索引,我索引id列,它是一个标识(1,1)列。我在id上创建了一个聚集索引,但这并没有解决我的问题。请问我该如何处理?

更新 以下是我的[ValidatePrevFVCollectorByDebtorID],请问这与该问题有关吗?

DECLARE @ID AS BIGINT

--LOOP ALL THE DEBTOR COLLECTOR HISTORY ID
----------------------------------------------------------------------------    
DECLARE LOOPDEBTOR_CURSOR CURSOR
    FOR  SELECT ID FROM DEBTOR WITH(NOLOCK) WHERE  ID = @REFDEBTORID
    --WHERE COLLECTORID > 0 

OPEN LOOPDEBTOR_CURSOR

FETCH NEXT FROM LOOPDEBTOR_CURSOR INTO @ID


WHILE @@FETCH_STATUS = 0
BEGIN
    DECLARE @COLLECTORYHISTORYID AS BIGINT
    DECLARE @ASSIGNON AS DATETIME
    DECLARE @DEBTORID AS BIGINT
    DECLARE @COLLECTORID AS BIGINT



    DECLARE @PREVCOLLECTORID AS BIGINT 

    --GET THE 1ST HISTORY ID BASE ON DEBTORID
    ---------------------------------------------------------------------------         
    DECLARE @FIRSTID AS BIGINT
    SET @FIRSTID = (SELECT TOP 1 ID FROM  FVOFFICER WITH(NOLOCK) 
        WHERE DEBTORID = @ID ORDER BY ID  )

    -- LIST ALL THE DEBTOR COLLECTOR HISTORY BASE ON DEBTORID = @ID
    ---------------------------------------------------------------------------         
    DECLARE LOOPHISTORY_CURSOR CURSOR
        FOR  SELECT ID,DEBTORID,COLLECTORID,ASSIGNON FROM FVOFFICER 
        WHERE DEBTORID = @ID ORDER BY ID ASC
    OPEN LOOPHISTORY_CURSOR

    FETCH NEXT FROM LOOPHISTORY_CURSOR INTO 
        @COLLECTORYHISTORYID,@DEBTORID,@COLLECTORID,@ASSIGNON

    WHILE @@FETCH_STATUS = 0
    BEGIN


        --FIX PREV COLLECTOR ID VALUE
        IF @FIRSTID != @COLLECTORYHISTORYID
            UPDATE  FVOFFICER SET PREVCOLLECTORID = @PREVCOLLECTORID 
            WHERE ID = @COLLECTORYHISTORYID                                                             

        SET @PREVCOLLECTORID = @COLLECTORID

    FETCH NEXT FROM LOOPHISTORY_CURSOR INTO 
        @COLLECTORYHISTORYID,@DEBTORID,@COLLECTORID,@ASSIGNON
    END

    CLOSE LOOPHISTORY_CURSOR
    DEALLOCATE LOOPHISTORY_CURSOR

FETCH NEXT FROM LOOPDEBTOR_CURSOR INTO @ID


END


CLOSE LOOPDEBTOR_CURSOR
DEALLOCATE LOOPDEBTOR_CURSOR

1 个答案:

答案 0 :(得分:0)

抱歉,我发布了错误的proc作为我的&#39; ValidatePrevFVCollectorByDebtorID&#39;。仔细考虑后。我知道锁定发生在

的ValidatePrevFVCollectorByDebtorID程序中
 DECLARE LOOPHISTORY_CURSOR CURSOR
        FOR  SELECT ID,DEBTORID,COLLECTORID,ASSIGNON FROM FVOFFICER 
        WHERE DEBTORID = @ID ORDER BY ID ASC
    OPEN LOOPHISTORY_CURSOR

债务人被锁定在上方。由于我在循环中执行批量插入,因此调用了新的插入实例,但是debtorid被锁定在前一个任务/光标上。首先

1我在select语句中添加了关键字with(nolock),如下所示

SELECT ID,DEBTORID,COLLECTORID,ASSIGNON FROM FVOFFICER with(nolock)
                WHERE DEBTORID = @ID ORDER BY ID ASC
  1. 然后我在表FVOfficer上的列debtorid上添加了一个非聚集索引,以提高我的select语句的速度。在我跑完之后,僵局并没有发生。