Stored Procedure failing on INSERT

Discussion in 'Databases' started by bkirkman56, Sep 15, 2008.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. I have a stored procedure that is failing during an INSERT operation.

    The error is a Foreign Key constraint that I have on the table. The procedureruns during the creation of an Order in my web app which first insertsan Order into the Order table returning the ID and then inserts orderitems into an OrderItems table linked on the order id andthe orderitemID that is an auto increment field. The Order inserts just fine but the Order Item fails to insert.

    I ran the stored procedure manually by doing 'execute procedure' in SMS and addingthe parameter data I was trying to insert. It completed with no error. It seems like the the transaction isolation level may not be set as I want. I am thinking its set to READ COMMITTED but I may want READ UNCOMMITTED.
    I am thinking this because it seems like the Order ID that is inserted and passed to the order item insert should be valid (I see it in the table) yet the FK constraint is saying "The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Order_OrderItem". The conflict occurred in database "MYDB", table "dbo_Order", column 'OrderID'. The statement has been terminated.
    The 'InsertOrderItem' procedure attempted to return a status of NULL, which is not allowed. A status of 0 will be returned instead.".

    Here is the FK DDL:



    ALTER TABLE [dbo].[OrderItem] WITH CHECK ADD CONSTRAINT [FK_Order_OrderItem] FOREIGN KEY([OrderID])
    REFERENCES [dbo].[Order] ([OrderID]) ON DELETE CASCADE


    Suggestions on how to correct this or further steps to analyze?
     
  2. Well, I went back and retraced the code and found that I was not setting the OrderId correctly so the second stored procedure failed because there was no '0' for the OrderId in the table. Definitely a "DOH!". Please disregard the posting above.
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page