1

在删除关联记录时,我的删除 Tsql 出现问题,有人可以帮我解决它吗

ALTER PROCEDURE [dbo].[Delete_Resource]
@ResID INT,

@UserID uniqueidentifier  

AS
BEGIN

    DELETE FROM [ideaPark_DB].[dbo].[Topics_Resources]
      WHERE [ideaPark_DB].[dbo].[Topics_Resources].ResourceID =  @ResID



    DELETE FROM [ideaPark_DB].[dbo].[Likes]
      WHERE [ideaPark_DB].[dbo].[Likes].resourceID = @ResID

     declare @BookmarkID  INT;
     SET  @BookmarkID  =  (SELECT [ideaPark_DB].[dbo].[Bookmarks].id  FROM [ideaPark_DB].[dbo].[Bookmarks] WHERE [ideaPark_DB].[dbo].[Bookmarks].resourceID = @ResID and Bookmarks.userID = @UserID)
     DELETE FROM [ideaPark_DB].[dbo].[Bookmarks]
      WHERE [ideaPark_DB].[dbo].[Bookmarks].resourceID = @ResID

      DELETE FROM [ideaPark_DB].[dbo].Bookmarks_Groups 
      WHERE Bookmarks_Groups.bookmarkID = @BookmarkID

     delete FROM [ideaPark_DB].[dbo].[Resources_Relationship]
      WHERE [ideaPark_DB].[dbo].[Resources_Relationship].fk_parent = @ResID


    DELETE FROM [ideaPark_DB].[dbo].[Skills_Resources]
      WHERE [ideaPark_DB].[dbo].[Skills_Resources].ResourceID = @ResID




     DELETE FROM [ideaPark_DB].[dbo].[Resources]
      WHERE [ideaPark_DB].[dbo].[Resources].Id  = @ResID 

END

错误

发生严重错误。DELETE 语句与 REFERENCE 约束“FK_Resources_Relationship_Resources1”冲突。冲突发生在数据库“ideaPark_DB”、表“dbo.Resources_Relationship”、列“fk_child”中。该语句已终止。-

4

2 回答 2

1

在最终删除之前添加以下内容Resources

delete FROM [ideaPark_DB].[dbo].[Resources_Relationship]
WHERE [ideaPark_DB].[dbo].[Resources_Relationship].fk_chid = @ResID

你的Resources_Relationship桌子上有两个 FK 回到Resources桌子上,你只处理其中一个。

于 2013-08-21T21:03:20.130 回答
0

我看到两个问题:

首先:

DELETE FROM [ideaPark_DB].[dbo].[Bookmarks]
WHERE [ideaPark_DB].[dbo].[Bookmarks].resourceID = @ResID

DELETE FROM [ideaPark_DB].[dbo].Bookmarks_Groups 
WHERE Bookmarks_Groups.bookmarkID = @BookmarkID

我会更改这些删除的顺序,因为 bookmark_groups 是书签和组之间的中间表。

第二:

删除后:

DELETE FROM [ideaPark_DB].[dbo].[Resources_Relationship]
WHERE [ideaPark_DB].[dbo].[Resources_Relationship].fk_parent = @ResID

你必须添加这个:

DELETE FROM [ideaPark_DB].[dbo].[Resources_Relationship]
WHERE [ideaPark_DB].[dbo].[Resources_Relationship].fk_child = @ResID
于 2013-08-21T22:15:50.203 回答