我对此代码有疑问:
ALTER PROCEDURE [arch].[spInsertToDocumentFileCategoryRelation]
@DocumentNumber nvarchar(50)
,@DocumentDate date
,@IsDocumentDelete bit
,@Address nvarchar(max)
,@DocumentLevelTypeId int
,@DocumentId2 bigint
,@DocumentRelationTypeId tinyint
,@CategoryId bigint
,@FileId bigint
,@FileAssignCategoryId bigint OUTPUT
,@DocumentRelationId bigint OUTPUT
,@FileOfDocumentId bigint OUTPUT
,@DocumentId bigint OUTPUT
AS
BEGIN
INSERT INTO [arch].[Document]
(
[DocumentNumber]
,[DocumentDate]
,[DateOfDocumentCreate]
,[IsDocumentDelete]
,[Address]
,[DocumentLevelTypeId]
)
VALUES
(
@DocumentNumber
,@DocumentDate
,'2015-5-12'
,@IsDocumentDelete
,@Address
,@DocumentLevelTypeId
)
Set @DocumentId = SCOPE_IDENTITY();
INSERT INTO [arch].[FileOfDocument]
(
[DocumentId]
,[FileId]
)
VALUES
(
@DocumentId
,@FileId
)
Set @FileOFDocumentId = SCOPE_IDENTITY();
INSERT INTO [arch].[DocumentRelation]
(
[DocumentRelationTypeId]
,[DocumentId1]
,[DocumentId2]
)
VALUES
(
@DocumentRelationTypeId
,@DocumentId
,@DocumentId2
)
Set @DocumentRelationId = SCOPE_IDENTITY();
INSERT INTO [arch].[FileAssignCategory]
(
[CategoryId]
,[FileId]
)
VALUES
(
@CategoryId
,@FileId
)
Set @FileAssignCategoryId = SCOPE_IDENTITY();
END
这段代码执行正确但是当我想用一些输入值测试这个存储过程时,sql server报告了一些错误:
Msg 515, Level 16, State 2, Procedure
spInsertToDocumentFileCategoryRelation, Line 27
Cannot insert the value NULL into column 'DocumentTypeId', table
'ffisherDB.arch.Document'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Msg 515, Level 16, State 2, Procedure
spInsertToDocumentFileCategoryRelation, Line 48
Cannot insert the value NULL into column 'DocumentId', table
'ffisherDB.arch.FileOfDocument'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Msg 515, Level 16, State 2, Procedure
spInsertToDocumentFileCategoryRelation, Line 61
Cannot insert the value NULL into column 'DocumentId1', table
'ffisherDB.arch.DocumentRelation'; column does not allow nulls. INSERT
fails.
The statement has been terminated.
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
我有四个用SCOPE_IDENTITY()设置的Key。第一个设置完全正确,但其他设置失败并且错误报告上方。
请帮帮我。
答案 0 :(得分:0)
好吧,从错误文本看,您的arch.Document
表格似乎有不可为空的列DocumentTypeId
。
你在这里没有填写这个专栏:
INSERT INTO [arch].[Document]
(
[DocumentNumber]
,[DocumentDate]
,[DateOfDocumentCreate]
,[IsDocumentDelete]
,[Address]
,[DocumentLevelTypeId]
)
所以这个列有它的默认值(我猜它是null),因此这个插入失败,因此@DocumentId
为空,剩下的插入也失败...
答案 1 :(得分:0)
根据DocumentTypeId
表中的第一条错误消息Document
字段不允许为空。您应该在插入查询中插入一个值。
将插入内容更改为
INSERT INTO [arch].[Document]
(
[DocumentNumber]
,[DocumentDate]
,[DateOfDocumentCreate]
,[IsDocumentDelete]
,[Address]
,[DocumentLevelTypeId]
,[DocumentTypeId] --Add document type id here
)
VALUES
(
@DocumentNumber
,@DocumentDate
,'2015-5-12' --You better insert a DATE type or in ISO formatted 'yyyymmdd' string.
,@IsDocumentDelete
,@Address
,@DocumentLevelTypeId
,@DocumentTypeId --Insert a non - null value
)
第二&第三条错误消息是由于第一次错误造成的。