节点 删除父节点时删除子节点的存储过程

alter procedure DelereNote
 @id int--定义要删除树节点
 as
declare @childID int--声明变量
 declare cursors cursor local for select id from test where pid=@id--local关键字 全局游标
 open cursors
  fetch next from cursors into @childID--取游标的值赋给变量
  while(@@FETCH_STATUS=0)--判断当前游标内是否存在值
  begin
    exec DelereNote @childID--递归调用存储过程 把孩子节点的值赋给游标 递归调用
    fetch next from cursors into @childID--继续读取游标里面的内容
  end  
  close cursors
  deallocate cursors
delete from test where ID=@id--存储过程执行的删除方法
 
exec DelereNote 6--调用储存过程对于递归调用不很容易理解 需要多加练习

 

。net交流
原文地址:https://www.cnblogs.com/hcyblogs/p/4703799.html