create procedure pro_delete
@deleteId int
as
declare @nodeid int
declare @id int
declare @pid int
set @nodeid = @deleteId
set @id = @nodeid
set @pid = @nodeid
begin transaction
while(exists (select * from H where id = @nodeid))
begin
if(exists (select * from H where Pid = @id and id != @id) )
begin
set @pid = @id
set @id = (select top 1 id from H where pid = @id and id != @id)
end
else
begin
print @id
delete from H where id = @id
set @id = @pid
set @pid = (select top 1 pid from H where id = @pid)
end
end
if(@@ERROR<>0)
ROLLBACK TRANSACTION
else
commit transaction
来源:https://www.cnblogs.com/caishuowen/archive/2011/04/29/2032984.html