首页 > 解决方案 > SCOPE_IDENTITY 有时返回空值

问题描述

我有一个 SQL 查询,有时第一个语句中的IDENTITYINSERT [Form]会返回NULL并导致批处理中的其余语句失败。

FK我包括了该过程的第二个块,以指示由于与表上的约束冲突而导致失败的地方Form

以前,它一直在使用SCOPE_IDENTITY,但我已将其更改为使用 SQL Server 的临时表和一个OUTPUT子句,以查看它是否会缓解问题,但没有。

此过程是从 ASP.NET Webforms 应用程序调用的,并且该调用由在该应用程序中运行的 Web API 调用启动。

以前,在将其转换为 Web API 调用之前,它是由 webforms 应用程序中的单击事件启动的。那时我会看到这个错误时不时地发生。

随着越来越多的应用程序使用和越来越重的负载,这似乎更频繁地发生。

我检查了一下,表上没有触发的触发器。我想不出任何方法来复制或追踪问题。

在大多数情况下,该程序可以正常工作,但有时却不能,我不确定为什么。有时,当用户尝试保存他们正在处理的内容时,我会看到连续多次发生的错误日志。如果他们尝试了足够多的时间,它似乎会奏效。

我已经研究过使用其他形式的身份检索,比如@@IDENTITY那些不能满足我的需要。

有什么我想念的吗?

ALTER PROCEDURE [dbo].[IC_Note_UpdateForm]
    @FormID int = -1 OUTPUT, @ConsultFormID int = -1 OUTPUT, @PatientSignature bit, @DSPSignature bit, @Editable bit, @Narrative nvarchar(MAX) = NULL, @SignificantIssues nvarchar(MAX), @UserID int, @DSPID int, @FormTypeID int, @ServiceID int, @ApptID int OUTPUT, @LocationID int, @LoggedInUser int, @PortalId int,@ClientNotes nvarchar(MAX)
WITH EXEC AS CALLER
AS
SET NOCOUNT ON;

--This is needed for whatever reason in order to get the correct date submitted. Using the function call inline causes weird stuff to happen 
DECLARE @DateSubmitted DATETIME = dbo.GetLocalTime(default)

DECLARE @count int
--See if a record exists for the Form
SELECT @count = COUNT(FormId) FROM Form WHERE (formID = @FormID OR (apptID = @ApptID AND apptID >= 1))

if @count > 0 BEGIN

    UPDATE dbo.Form SET 
        FormTypeID = @FormTypeID,
        patientSignature = @PatientSignature,
        dspSignature = @DSPSignature,
        editable = @Editable,
        dateSubmitted = @DateSubmitted
    WHERE 
        formID = @FormID

    IF @Editable = 0 BEGIN
        exec IC_NoteAudit_Insert @FormId, @DSPID, 'SUBMITTED'
    END ELSE BEGIN 
        exec IC_NoteAudit_Insert @FormID, @DSPID, 'UPDATED'
    END

END ELSE BEGIN
    DECLARE @tempForm TABLE (FormId int) 

    INSERT dbo.Form (
        PortalId
        ,userID
        ,dspID
        ,dateSubmitted
        ,patientSignature
        ,dspSignature
        ,editable
        ,approved
        ,dateApproved
        ,rejected
        ,formTypeID
        ,paid
        ,billed
        ,serviceID
        ,apptID
    ) OUTPUT inserted.formId INTO @tempForm
        VALUES (
            @PortalId
        ,@UserID   -- userID - int
        ,@DSPID   -- dspID - int
        ,@DateSubmitted  -- dateSubmitted - datetime
        ,@PatientSignature  -- patientSignature - bit
        ,@DSPSignature  -- dspSignature - bit
        ,@Editable  -- editable - bit
        ,null  -- approved - bit
        ,null  -- dateApproved - datetime
        ,null  -- rejected - bit
        ,@FormTypeID   -- formTypeID - int
        ,0  -- paid - bit
        ,0  -- billed - bit
        ,@ServiceID   -- serviceID - int
        ,@ApptID   -- apptID - int
    )

    --This was SET @FormId = SCOPE_IDENTITY() before and had the same NULL FK constraint occur 
    SET @FormID = (SELECT TOP 1 FormId FROM @tempForm)

END

--Move these out of scope of the IDENTITY retrieval 
IF @count = 0 BEGIN 
    exec IC_NoteAudit_Insert @formID, @DSPID, 'CREATED'

    IF @Editable = 0 BEGIN
        exec IC_NoteAudit_Insert @formID, @DSPID, 'SUBMITTED'
    END
END

SELECT @count = COUNT(FormId) FROM ConsultForm WHERE formId = @FormID
IF @count > 0 BEGIN

--See if a row exists for the ConsultForm
UPDATE dbo.ConsultForm SET 
    narrative = @Narrative,
    significantIssues = @SignificantIssues
WHERE 
    consultFormID = @ConsultFormID
    AND formID = @FormID

END ELSE BEGIN
    DECLARE @tempConsultForm TABLE (ConsultFormId int)
    INSERT dbo.ConsultForm (
        PortalId
        ,formID
        ,dateOfService
        ,timeIn
        ,timeOut
        ,narrative
        ,significantIssues
        ,locationOfService
    ) OUTPUT inserted.ConsultFormID INTO @tempConsultForm
    VALUES (
        @PortalId,
        @FormID    -- formID - int
        ,null  -- dateOfService - datetime
        ,null  -- timeIn - datetime
        ,null  -- timeOut - datetime
        ,@Narrative -- narrative - nvarchar(MAX)
        ,@SignificantIssues -- significantIssues - nvarchar(MAX)
        ,null -- locationOfService - nvarchar(MAX)
    )

    /*** Failure with FK constraint happens here, @FormId is NULL ***/

    SET @ConsultFormID = (SELECT TOP 1 ConsultFormId FROM @tempConsultForm)

END ````

标签: sqlsql-server

解决方案


哪个版本的 SQL,似乎是 SQL 中的一个错误。您可以在 SQL 代码中使用 RAISERROR 来确定它是否是 SQL 本身的问题。


推荐阅读