删除父节点及父节点之下的全部节点
比如删除节点为3,将递归删除其下的全部节点6,11,12
表里面需要创建ID ,PID TID用处以后对树的复制操作 表格式如图所示:
PID用于存放节点的值 name存放内容 ID自动增长
0节点存在ID为1的节点 1节点下存在ID为2,3的节点 依次类推....
递归删除节点:
create proc DelereNote
(
@id int--定义要删除树节点
)
as
begin
declare @childID int--声明变量
declare cursors cursor local for select 表ID from 表 where 对应ID=@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 表 where 表ID=@id--存储过程执行的删除方法
end
go
exec DelereNote 2--调用储存过程对于递归调用不很容易理解 需要多加练习
查看某个节点下所有节点
with myT2 as(
select * from System_Column Where Column_ID=10
union all
select System_Column.* from myT2 inner join System_Column on myT2.Column_ID=System_Column.Column_Aid)
select * from myT2