2015-07-02 2 views
-1

У меня есть проблема с этим кодом:Ошибка вставки в базу данных в хранимых процедурах

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(). первый из них настроен точно, но остальные терпят неудачу и выше отчета об ошибках.

, пожалуйста, помогите мне.

+0

'Невозможно вставить значение NULL в столбец 'DocumentTypeId', таблицу 'ffisherDB.arch.Document'; столбец не допускает ошибок. «Говорит все это. –

ответ

0

Ну, из текста ошибки это выглядит так, как ваш стол arch.Document имеет столбец с нулевым значением DocumentTypeId.

И вы не заполнить этот столбец здесь:

INSERT INTO [arch].[Document] 
    (
    [DocumentNumber] 
    ,[DocumentDate] 
    ,[DateOfDocumentCreate] 
    ,[IsDocumentDelete] 
    ,[Address] 
    ,[DocumentLevelTypeId] 
    ) 

Так что этот столбец имеет значение по умолчанию (я предполагаю, что это нуль), таким образом, эта вставка не может, таким образом @DocumentId является недействительным и остальные вставки не может слишком ...

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 
     ) 

Вторых & сообщений третьей ошибки происходит из-за первую ошибку.

Смежные вопросы