超出最大存储过程,函数,触发器或视图嵌套级别(限制32)。

时间:2016-11-29 17:47:38

标签: sql-server

我有3个表状态(sehir),城市(ilce)和邻居(semtmah),我添加了以下触发器,这样当我删除状态时,应删除与之相关的所有城市和社区

Create trigger [dbo].[sehir_sil]
on [dbo].[Sehirler]
  instead of delete
  as 
begin
set nocount on
declare @id int

select @id = Sehirler.SehirId from Sehirler
delete from Ilceler where SehirId = @id
end

CREATE trigger [dbo].[ilce_sil]
on [dbo].[Ilceler]
  instead of delete
  as 
begin
set nocount on
declare @id1 int

select @id1 = Ilceler.ilceId from Ilceler
delete from SemtMah where SemtMahId = (select top 1 SemtMahId  from semtmah where ilceId = @id1 order by SemtMahId)
end

Create trigger [dbo].[semtmah_sil]
on [dbo].[SemtMah]
  instead of delete
  as 
begin
set nocount on
declare @id2 int
declare @idsehir int

select @id2 = SemtMah.ilceId from SemtMah
select @idsehir = Ilceler.SehirId from Ilceler where ilceId = @id2
delete from SemtMah where ilceId in (select ilceId from Ilceler where SehirId = @idsehir)
delete from Ilceler where SehirId =@idsehir
delete from Sehirler where SehirId = @idsehir
end

执行查询时

delete from Sehirler where SehirId = 17

我在"过程ilce_sil,第10行[Batch Start Line 0]"上面得到了错误。虽然我试图在第10行(semtmah_sil)触发一次声明。我的Recursive_triggers已经关闭了 关于如何解决它的任何建议?

1 个答案:

答案 0 :(得分:0)

逻辑完全有缺陷但是由于@JustinCave

我实现了我想要的级联删除