2011-01-13 15 views
0

以下のストアドプロシージャに問題があります。最後の挿入INSERTをコメント解除しない限り、正常に動作します。私は最後のINSERTのコメントを解除した場合、私は次のエラーを取得する:私は私が得るrecipeTagテーブルに挿入しようとするのであればSQL Serverでの単純なストアドプロシージャ:ブリッジテーブルへの挿入

recipe(id, title, introduction, directions) 
recipeTag(id, recipeID, tagID) 
tag(id, name) 

明確にするため

Msg 547, Level 16, State 0, Procedure InsertRecipeWithTags, Line 42 The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Tag_TagRecipe". The conflict occurred in database "RecipeBox", table "RecipeDetails.Tag", column 'tagID'. The statement has been terminated.

、私は3つのテーブルを持っています上記のエラー。お知らせ下さい。

ありがとうございました。

CREATE PROCEDURE [RecipeDetails].[InsertRecipeWithTags] 
/* 
    variables that map to columns 
*/ 
@title varchar(50), 
@intro varchar(255), 
@directions varchar(2200), 
@ingredients varchar(2200), 
@difficulty varchar(6), /*need check constraint setting difficulty to "beginner" "medium" or "expert"*/ 
@prepTimeHour tinyint, 
@prepTimeMinute tinyint, 
@inactiveTimeHour tinyint, 
@inactiveTimeMinute tinyint, 
@servings tinyint, 
@photo varbinary(MAX), 
@tagName varchar(50), 
@tagdescription varchar(255) 

AS 

BEGIN 
    SET NOCOUNT ON; 

    DECLARE @RecipeID int, @TagID int 

    INSERT INTO RecipeDetails.Recipe (title, intro,directions, ingredients, difficulty, 
    prepTimeHour, prepTimeMinute, inactiveTimeHour, inactiveTimeMinute, servings, photo) 
    VALUES (@title, @intro,@directions, @ingredients, @difficulty, @prepTimeHour, @prepTimeMinute, 
    @inactiveTimeHour, @inactiveTimeMinute, @servings, @photo) 

    SELECT @RecipeID=SCOPE_IDENTITY() 

    SELECT * FROM RecipeDetails.Recipe WHERE recipeID = @RecipeID; 

    INSERT INTO RecipeDetails.Tag (name, description) 
    VALUES (@tagName, @tagdescription) 

    SELECT @TagID=SCOPE_IDENTITY() 

    SELECT * FROM RecipeDetails.Tag WHERE tagID = @TagID; 

    /*INSERT INTO RecipeDetails.TagRecipe (tagID, recipeID) 
    VALUES (@RecipeID, @TagID)*/ 
END 

答えて

3

注文を元に戻しますか?

INSERT INTO RecipeDetails.TagRecipe (tagID, recipeID) 
VALUES (@TagID, @RecipeID) 

あなたはそれだ周り

+0

間違った方法を持っていました!ありがとう!私はまだこのようなものには非常に新しいです、そして、それは助けになりました! –

関連する問題