我需要通过存储过程在SQL Server 2008中运行三个Insert查询,并期望在C#中读取三个OUTPUT值。在以下存储过程中,我的第一个查询仅运行并且仍然是我收到Null错误
无法将值NULL插入列中 'AssessmentElectronicSignatureID'
USE [myDB]
GO
/****** Object: StoredProcedure [dbo]. [p_assessment_dfn_statementAnswer_insert] Script Date: 10/05/2015 13:24:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[p_assessment_dfn_statementAnswer_insert]
@StatementID AS bigint,
@StaffID AS int,
@AssessmentID As bigint,
@StatementText AS nvarchar(MAX),
@StatementDate AS Date,
@StatementAnswerID AS bigint OUTPUT,
--
@SignatureCheck AS bit,
@SignatureDate AS Date,
@ElectronicSignatureID AS bigint OUTPUT,
--
@AssessmentElectronicSignatureID AS bigint OUTPUT
AS
SET NOCOUNT ON
SET XACT_ABORT ON
-- local variables
DECLARE @l_object AS SYSNAME = OBJECT_NAME(@@PROCID)
,@l_error_msg AS NVARCHAR(2000)
BEGIN TRY
BEGIN TRAN
INSERT INTO [adb_TestDb].[dbo].[Assessment_Statement_Answer]
([StatementID],[StaffID],[AssessmentID],[StatementText],[Date])
VALUES (@StatementID, @StaffID, @AssessmentID, @StatementText, @StatementDate)
SELECT @StatementAnswerID = SCOPE_IDENTITY();
IF(@StatementAnswerID>0)
BEGIN
INSERT INTO [adb_TestDb].[dbo].[Assessment_ElectronicSignature]([AssessmentID],[ElectronicSignatureCheck],[SignatureDateAndTime])
VALUES (@AssessmentID, @SignatureCheck,@SignatureDate)
SELECT @ElectronicSignatureID = SCOPE_IDENTITY();
END
IF(@ElectronicSignatureID>0)
BEGIN
INSERT INTO [adb_TestDb].[dbo].[AssessorSignature]([AssessmentElectronicSignatureID],[StatementAnswerID],[AssessorID])
VALUES(@ElectronicSignatureID, @StatementAnswerID, @AssessmentID)
SELECT @AssessmentElectronicSignatureID = SCOPE_IDENTITY();
END
COMMIT TRAN
--RETURN SCOPE_IDENTITY();
END TRY
BEGIN CATCH
-- rollback any open/uncomitted transactions
IF XACT_STATE() IN ( -1, 1) ROLLBACK TRANSACTION
-- return an error containing the object, error number and error description
SELECT @l_error_msg = 'Error number : ' + CAST(ERROR_NUMBER()AS VARCHAR) + '. ' + ERROR_MESSAGE()
RAISERROR (@l_error_msg,16,1)
END CATCH
答案 0 :(得分:2)
SCOPE_IDENTITY()存在已知错误;使用触发器时偶尔发生。如果你想深入了解谷歌的出现。还是......
尝试更改
等内容SELECT @StatementAnswerID = SCOPE_IDENTITY();
到
SELECT @StatementAnswerID = @@IDENTITY;
或者
SET @StatementAnswerID = @@IDENTITY;
和其他类似的作业。我认为这里的问题是由于@StatementAnswerID为Null而引起的。