Sql 范围\标识有时返回空值

Sql 范围\标识有时返回空值,sql,sql-server,Sql,Sql Server,我有一个SQL查询,其中第一条语句中的INSERT[Form]中的IDENTITY值返回为NULL,并导致批处理中的其余语句失败 我包含了程序的第二个块,以指示由于与表表单上的FK约束冲突而失败的位置 以前,它一直在使用SCOPE\u IDENTITY,但我已将其更改为使用SQL Server的临时表和OUTPUT子句,以查看它是否会缓解问题,但事实并非如此 正在从ASP.NET webforms应用程序调用此过程,该调用由该应用程序中运行的Web API调用启动 在此之前,它是由webform

我有一个SQL查询,其中第一条语句中的
INSERT[Form]
中的
IDENTITY
值返回为
NULL
,并导致批处理中的其余语句失败

我包含了程序的第二个块,以指示由于与表
表单
上的
FK
约束冲突而失败的位置

以前,它一直在使用
SCOPE\u IDENTITY
,但我已将其更改为使用SQL Server的临时表和
OUTPUT
子句,以查看它是否会缓解问题,但事实并非如此

正在从ASP.NET webforms应用程序调用此过程,该调用由该应用程序中运行的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 ````

哪个版本的SQL,看起来像是SQL中的一个bug。能否在SQL代码中使用RAISERROR来确定这是否是SQL本身的问题。

表单表是否有标识列?“这里有点不对劲,但我看不太清楚它是什么。”SeanLange当然知道!它也有很多记录,大约160000+假设它是最新的,因为这个数据库正在Azure中运行。该错误在ASP.NET代码中作为SqlException出现,因此必须由过程生成,否则不会引发SqlException。