아래 저장 프로 시저에 문제가 있습니다. 마지막 삽입 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.
, 나는 세 개의 테이블을 가지고 위의 오류. 제발 조언.
감사합니다.
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
을했다! 감사! 나는 아직도이 물건에 아주 새롭다. 그리고 그것은 도움이되었다! –