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

posted on 2011-04-29 17:14  轻盈  阅读(209)  评论(0编辑  收藏  举报