我正在尝试创建一个删除触发器。我有这个名为Nemanet_Navigation的导航表。该表有自己的外键,但在选择INSERT和UPDATE规范中的表的关系时 - 删除规则NO Action。所以我不选择Cascading。然后我有这个触发器:
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'。
这是因为table有一个FOREIGN KEY WITH cascading DELETE。但我的Nemante_Navigation表有删除规则 - 没有动作(但由于某种原因我不知道是标记为灰色 - 我无法改变它)。触发器位于Nemanet_Navigation表下的Triggers文件夹中。有人可以帮忙吗?
答案 0 :(得分:2)
INSTEAD OF
触发器用于修改您无法使用的视图DML
。
尝试使用BEFORE
或AFTER
触发器。