我有一个表ParentTable
(id, Name, town
)及其子表human
(mother, father
),其中两列都包含父表的映射。
父表的列town
包含带有Town
(townid, townname
)的外键引用,并且该城镇添加了外键引用。
create trigger DEL_Parent
ON Parent
instead of delete as
set nocount on
delete from human
where mother IN (select id from deleted)
or father IN (select id from deleted)
delete from Parent where id in(select id from deleted).
但是当我尝试执行触发器时,系统会显示以下错误
无法创建而不是删除或代替更新触发器 父表上的DEL_Parent。这是因为桌子有外国人 带有级联删除或更新的密钥。
如何解决问题?提前谢谢。
答案 0 :(得分:2)
将查询修改为:
create trigger DEL_Parent
ON Parent
after delete as
set nocount on
delete
from human
where mother IN (select id from deleted)
or father IN (select id from deleted)
delete
from Parent
where id in(select id from deleted)
因为无法在具有使用INSTEAD OF DELETE
操作定义的外键的表上定义DELETE
触发器。
答案 1 :(得分:0)
无法在具有使用DELETE操作定义的外键的表上定义INSTEAD OF DELETE触发器。
来源:http://msdn.microsoft.com/en-us/library/ms191208(v=sql.90).aspx
所以,我想如果你想要,你必须放弃你的cascade delete
并用触发器处理它,如果你还想强制执行它。