mysql递归表删除_只得到一条数据删除下级节点及下下级._删除无限级菜单的节点(递归)...

--这是一个无限级菜单的数据结构

--di int

--topic_id int

--msg varchar(30)

--topic_id对应同一个表中已经存在的ID,如果为0表示根节点

--现在想要删除关联的数据,删除指定id纪录的同时要删除topic_id指向这个id的纪录,

--以及被删除的子节点的关联节点,即topic_id指向子节点的纪录,如此反复

--传入的id可能是数字,也可能是形如“0,1,2,3”这样的字符串

create table TableMenu(aID int,topic_id int)

--测试数据

insert into TableMenu select 1,0

insert into TableMenu select 2,1

insert into TableMenu select 3,1

insert into TableMenu select 4,2

insert into TableMenu select 5,2

insert into TableMenu select 6,3

insert into TableMenu select 7,4

insert into TableMenu select 8,3

insert into TableMenu select 9,7

go

create proc DelNode

@id int

as

declare @FilterID int

delete from TableMenu where aid=@id

if exists(select aid,topic_id from TableMenu where topic_id=@id)

begin

select @FilterID=aid from TableMenu where topic_id=@id

--递归

exec DelNode @FilterID

end

go

--测试执行

exec DelNode 4

--所影响的行数为:

--(所影响的行数为 1 行)

--(所影响的行数为 1 行)

--(所影响的行数为 1 行)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值