Stored Procedure for Multi-Table Insert Error: Cannot Insert the Value Null into Column

Posted by SidC on Stack Overflow See other posts from Stack Overflow or by SidC
Published on 2010-05-06T05:29:35Z Indexed on 2010/05/06 5:38 UTC
Read the original article Hit count: 293

Good Evening All,

I've created the following stored procedure:

CREATE PROCEDURE AddQuote
-- Add the parameters for the stored procedure here

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
Declare @CompanyName nvarchar(50),
    @Addr nvarchar(50),
    @City nvarchar(50),
    @State nvarchar(2),
    @Zip nvarchar(5),
    @NeedDate datetime,
    @PartNumber float,
    @Qty int
-- Insert statements for procedure here
Insert into dbo.Customers
(CompanyName, Address, City, State, ZipCode)
Values (@CompanyName, @Addr, @City, @State, @Zip)

Insert into dbo.Orders 
(NeedbyDate)
Values(@NeedDate) 

Insert into dbo.OrderDetail
(fkPartNumber,Qty)
Values (@PartNumber,@Qty)


END
GO

When I execute AddQuote, I receive an error stating:

Msg 515, Level 16, State 2, Procedure AddQuote, Line 31
Cannot insert the value NULL into column 'ID', table 'Diel_inventory.dbo.OrderDetail'; column does not allow nulls. INSERT fails.
The statement has been terminated.

I understand that I've set Qty field to not allow nulls and want to continue doing so. However, are there other syntax changes I should make to ensure that this sproc works correctly?

Thanks, Sid

© Stack Overflow or respective owner

Related posts about sql-server-2005

Related posts about stored-procedures