我正在尝试创建删除触发器。我有一个名为 Nemanet_Navigation 的导航表。该表本身有一个外键,但是在为表选择关系时,我在 INSERT 和 UPDATE Specification 中选择了 - Delete Rule NO Action。所以我不选择级联。然后我有这个触发器:
CREATE TRIGGER Del_Nemanet_Navigation ON Nemanet_Navigation
INSTEAD OF DELETE
AS
CREATE TABLE #Table(
Nav_ID uniqueidentifier
)
INSERT INTO #Table (Nav_ID)
SELECT Nav_ID
FROM deleted
DECLARE @C uniqueidentifier
SET @c = 0
WHILE @C <> (SELECT COUNT(Nav_ID) FROM #Table) BEGIN
SELECT @c = COUNT(Nav_ID) FROM #Table
INSERT INTO #Table (Nav_ID)
SELECT Nemanet_Navigation.Nav_ID
From Nemanet_Navigation
LEFT OUTER JOIN #Table ON Nemanet_Navigation.Nav_ID = #Table.Nav_ID
WHERE Nemanet_Navigation.Nav_pID IN (SELECT Nav_ID FROM #Table)
AND #Table.Nav_ID IS NULL
END
DELETE Nemanet_Navigation
FROM Nemanet_Navigation
INNER JOIN #Table ON Nemanet_Navigation.Nav_ID = #Table.Nav_ID
但我收到以下错误:
无法在“Nemanet_Navigatin”上创建 INSTEAD OF DELETE 触发器“Del_Nemanet_Navigation”。
这是因为表有一个 FOREIGN KEY WITH cascading DELETE。但是我的 Nemante_Navigation 表有删除规则 - 没有操作(但由于某种我不知道的原因被标记为灰色 - 我无法更改它)。触发器位于 Nemanet_Navigation 表下名为 Triggers 的文件夹中。有人可以帮忙吗?