日期:2014-05-18  浏览次数:20541 次

关于触发器递归死循环的问题
CREATE TRIGGER delfiled on tablename1 FOR DELETE 
AS
declare @id int,@ename varchar(50),@strdrop varchar(200)

select @id=id from deleted

select @ename=ename from deleted

delete from tablename1 where fatherid=@id

set @strdrop='alter table tablename2 drop column '+@ename+''

exec (@strdrop)

这个触发器产生了死循环,怎样修改才能避免此循环,谢谢

------解决方案--------------------
SQL code
CREATE TRIGGER delfiled  on tablename1 FOR  DELETE  
AS 
IF @@ROWCOUNT = 0
    RETURN
declare @id int,@ename varchar(50),@strdrop varchar(200) 

select @id=id from deleted 

select @ename=ename from deleted 

delete from tablename1 where fatherid=@id 

set @strdrop= 'alter table tablename2  drop column   '+@ename+ ' ' 

exec (@strdrop)