参考一下这个用户定义函数:
---------------------------------------------------------
--生成测试数据
create table BOM(ID VARCHAR(10),PID VARCHAR(10))
insert into BOM select 'a',NULL
insert into BOM select 'b','a'
insert into BOM select 'c','a'
insert into BOM select 'd','b'
insert into BOM select 'e','b'
insert into BOM select 'f','c'
insert into BOM select 'g','c'
go--创建用户定义函数
create function f_getChild(@ID VARCHAR(10))
returns @t table(ID VARCHAR(10),PID VARCHAR(10),Level INT)
as
begin
declare @i int,@ret varchar(8000)
set @i = 1
insert into @t select ID,PID,@i from BOM where PID = @ID
while @@rowcount<>0
begin
set @i = @i + 1
insert into @t
select
a.ID,a.PID,@i
from
BOM a,@t b
where
a.PID=b.ID and b.Level = @i-1
end
return
end
go--执行查询
select * from dbo.f_getChild('a')
go--输出结果
/*
b a 1
c a 1
d b 2
e b 2
f c 2
g c 2
*/--删除测试数据
drop function f_getChild
drop table BOM
---------------------------------------------------------
--生成测试数据
create table BOM(ID VARCHAR(10),PID VARCHAR(10))
insert into BOM select 'a',NULL
insert into BOM select 'b','a'
insert into BOM select 'c','a'
insert into BOM select 'd','b'
insert into BOM select 'e','b'
insert into BOM select 'f','c'
insert into BOM select 'g','c'
go--创建用户定义函数
create function f_getChild(@ID VARCHAR(10))
returns @t table(ID VARCHAR(10),PID VARCHAR(10),Level INT)
as
begin
declare @i int,@ret varchar(8000)
set @i = 1
insert into @t select ID,PID,@i from BOM where PID = @ID
while @@rowcount<>0
begin
set @i = @i + 1
insert into @t
select
a.ID,a.PID,@i
from
BOM a,@t b
where
a.PID=b.ID and b.Level = @i-1
end
return
end
go--执行查询
select * from dbo.f_getChild('a')
go--输出结果
/*
b a 1
c a 1
d b 2
e b 2
f c 2
g c 2
*/--删除测试数据
drop function f_getChild
drop table BOM
create table BOM(ID INT,Topic_ID INT,MSG VARCHAR(1000))
insert into BOM select 1,0,NULL
insert into BOM select 2,1,NULL
insert into BOM select 3,1,NULL
insert into BOM select 4,2,NULL
insert into BOM select 5,3,NULL
insert into BOM select 6,5,NULL
insert into BOM select 7,6,NULL
go--创建用户定义函数
create function f_getChild(@ID VARCHAR(10))
returns @t table(ID VARCHAR(10),Topic_ID VARCHAR(10),Level INT)
as
begin
declare @i int,@ret varchar(8000)
set @i = 1
insert into @t select ID,Topic_ID,@i from BOM where ID = @ID
while @@rowcount<>0
begin
set @i = @i + 1
insert into @t
select
a.ID,a.Topic_ID,@i
from
BOM a,@t b
where
a.Topic_ID=b.ID and b.Level = @i-1
end
return
end
go--创建用户定义存储过程
create procedure sp_relationDel(@var varchar(100))
as
begin
declare @ID varchar(100)
while charindex(',',@var)>0
begin
set @ID = left(@var,charindex(',',@var)-1)
set @var = stuff(@var,1,charindex(',',@var),'')
delete BOM where ID in(select ID from dbo.f_getChild(@ID))
end
delete BOM where ID in(select ID from dbo.f_getChild(@var))
end
go--执行删除操作
exec sp_relationDel '3,5,6'--查看执行结果
select * from BOM/*
ID Topic_ID MSG
---- ---------- -----
1 0 NULL
2 1 NULL
4 2 NULL
*/--删除测试数据
drop procedure sp_relationDel
drop function f_getChild
drop table BOM
insert into tb select 1,0
insert into tb select 2,1
insert into tb select 3,1
insert into tb select 4,2
insert into tb select 5,2
insert into tb select 6,3
insert into tb select 7,3
go
--创建用户定义函数
create function f_getChild(@ID int)
returns @tb table(ID int,topic_id int)
as
begin
insert @tb select ID,topic_id from tb where topic_id = @ID
while @@rowcount<>0
begin
insert @tb
select
a.ID,a.topic_id
from
tb a,@tb b
where
a.topic_id=b.ID
and
not exists(select 1 from @tb where ID=a.ID)
end
return
end
go--创建存储过程
create procedure deleteNodes
@IDList varchar(1000) --要删除的id列表
as
declare @tb table
(
ID int,
num int identity
)
while charindex(',',@IDList)>0
begin
insert @tb(ID) select left(@IDList,charindex(',',@IDList)-1)
set @IDList=stuff(@IDList,1,charindex(',',@IDList),'')
end
insert @tb(ID) select @IDListset xact_abort on
begin tran
declare @i int,@tmp int
set @i=1while @i<=(select max(num) from @tb)
begin
select @tmp=ID from @tb where num=@i
delete from tb where exists(select 1 from f_getChild(@tmp) where ID=tb.ID)
set @i=@i+1
endcommit trango--测试
exec deleteNodes '3'/*
ID topic_id
----------- -----------
1 0
2 1
3 1
4 2
5 2(所影响的行数为 5 行)
*/
exec deleteNodes '2,1'/*
ID topic_id
----------- -----------
1 0(所影响的行数为 1 行)
*/--删除测试环境
drop function f_getChild
drop procedure deleteNodes
drop table tb
@idi int
as
declare @id int
declare @topic varchar(10)
declare Del_Cursor cursor local for
select idi,topic_id from table1 where idi=@idi
open Del_Cursor
fetch next from Del_Cursor
into @id,@topicid
while @@fetch_status=0
begin
delete from table1 where idi=@id
delete from table1 where topic_id=@topic_id
fetch next from Del_Cursor
into @id,@topicid
end
close Del_Cursor
deallocate Del_Cursor
go-- 或者二
Delect From Table1 Where ID = @ID Or Topic_ID = @ID
--测试数据
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 行)