存储过程:create proc proc_name
@id int
asdeclare @temp table (id int)insert @temp values (@id)while exists (
select autoid from tablename
where fatherid in (select id from @temp)
and autoid not in (select id from @temp)
)
insert @temp 
select autoid from tablename
where fatherid in (select id from @temp)
and autoid not in (select id from @temp)delete tablename
where  autoid in (select id from @temp)go

解决方案 »

  1.   

    写触发器也可以,但是也要将全部的子节点都删除。
    如果只删除子节点,不会自动再调用触发器删除删除下一层节点。触发器代码与杨的类似,只是将@id换成删除表中的id而已。
      

  2.   

    建议不这样设计表,设计一个主键表tree(id[主健],name)
    一个外键表rational(treeid[关联到tree表的id],fatherid[关联到tree表的id])
    一个tree for delete的触发器这样你在删除tree表记录时,就可以删除rational表的相应记录了
      

  3.   

    注意触发器应该这样写,确保在删除操作之前触发CREATE TRIGGER treedelete ON [dbo].[tree] 
    INSTEAD OF  DELETE  
    AS
    delete from rational where (treeid in (select id from deleted)) or (fatherid in (select id from deleted))
      

  4.   

    如果是MS SQL 2000 的话:
    把autoid、 fatherid 做一个表的主外兼连接。然后选中级连删除就可以了。
      

  5.   

    我的实现通过“笨”方法解决了。
    我是分三步实现的:第一:连接库,先删除autoid或者fatherid为某一指定值的,关闭库;
    第二:连接库,把所有的autoid读出来存为一个数组,关闭库;
    第三:连接库,删除所有fatherid not in (数组),关闭库。如果三步都不出错的话(谁敢做这样的保证?),就
    这样总算可以解燃眉之急了,权益之计。望各位大虾指教出更为合理的办法。
      

  6.   

    用 Yang_(扬帆破浪) 的  方法
      

  7.   

    TO:laosan(老三) 
    你的做法这么麻烦,而且也看不出你如何保证不留垃圾在里面,好像还是只能删除两层。
      

  8.   

    为什么不用Yang_(扬帆破浪) 的存储过程呢?
      

  9.   

    CSDNM(CSDN经理(信就不假) :说的很有道理。确实问题大。
      

  10.   

    触发器create trigger nodedel on tree
    for delete
    asdeclare @temp table (id int)
    declare @id int
    select @id=autoid from deletedinsert @temp values (@id)while exists (
    select autoid from tree
    where fatherid in (select id from @temp)
    and autoid not in (select id from @temp)
    )
    insert @temp 
    select autoid from tree
    where fatherid in (select id from @temp)
    and autoid not in (select id from @temp)delete tree
    where  autoid in (select id from @temp)go
      

  11.   

    示例create table tree
    (autoid int,name varchar(10),fatherid int)insert into tree values(1,'a1',0)
    insert into tree values(2,'a2',0)
    insert into tree values(3,'a3',0)insert into tree values(4,'a11',1)
    insert into tree values(5,'a12',1)
    insert into tree values(6,'a13',1)insert into tree values(7,'a21',2)
    insert into tree values(8,'a22',2)insert into tree values(9,'a111',4)
    insert into tree values(10,'a112',4)
    insert into tree values(11,'a113',4)insert into tree values(12,'a121',5)
    insert into tree values(13,'a122',5)select * from tree order by namedelete from tree where autoid=1select * from tree order by name
      

  12.   

    use test
    if exists(select name from sysobjects where name='tree' and type='u')
    drop table tree
    create table tree
    (autoid int,name varchar(10),fatherid int)insert into tree values(1,'a1',0)
    insert into tree values(2,'a2',0)
    insert into tree values(3,'a3',0)insert into tree values(4,'a11',1)
    insert into tree values(5,'a12',1)
    insert into tree values(6,'a13',1)insert into tree values(7,'a21',2)
    insert into tree values(8,'a22',2)insert into tree values(9,'a111',4)
    insert into tree values(10,'a112',4)
    insert into tree values(11,'a113',4)insert into tree values(12,'a121',5)
    insert into tree values(13,'a122',5)
    -------------------------------------------------------------------------------------
    --設要刪除的行autoid=9.你可以把下面的代碼改為存儲過程﹐入口參數為@inautoid
    declare @autoid int,
                 @fatherid int,
                 @count int,
                 @inautoid intselect @inautoid=9select @autoid=autoid,@fatherid=fatherid from tree
    where autoid=@inautoiddelete tree
    where autoid=@inautoidselect @inautoid=@fatheridselect @count=count(*) from tree
    where autoid=@inautoidwhile @count<>0
    begin
    select @count=count(*) from tree
    where autoid=@inautoidif @count=0 begin break end
    select @autoid=autoid,@fatherid=fatherid from tree
    where autoid=@inautoiddelete tree
    where autoid=@inautoidselect @inautoid=@fatherid
    end
      

  13.   

    查看SQL2K ONLINE BOOK,有如下信息:ON DELETE CASCADE指定如果试图删除某行,而该行含有由其它表的现有行中的外键所引用的键,则也将删除所有包含那些外键的行。如果在目标表上也定义了级联引用操作,则对从那些表中删除的行同样采取指定的级联操作。ON UPDATE CASCADE指定如果试图更新某行中的键值,而该行的键值由其它表的现有行中的外键所引用,则所有外键值也将更新成为该键指定的新值。如果在目标表上也定义了级联引用操作,则对在那些表中更新的键值同样采取指定的级联操作。
    是不是你要的?试试吧.
      

  14.   

    http://www.csdn.net/expert/topic/1076/1076010.xml?temp=.9044611
      

  15.   

    继续苦等高手进一步支持ing................
      

  16.   

    我来解释YANG_的存储过程,只有这么做才可以删干净,否则都是自欺欺人
    所以第一步建议存储过程:create proc proc_name
    @id int
    asdeclare @temp table (id int)insert @temp values (@id)while exists (
    select autoid from tablename
    where fatherid in (select id from @temp)
    and autoid not in (select id from @temp)
    )
    insert @temp 
    select autoid from tablename
    where fatherid in (select id from @temp)
    and autoid not in (select id from @temp)delete tablename
    where  autoid in (select id from @temp)go
    第二步建立触发器
    create trigger treedelete on tablename 
    after delete  
    as
    begin
      declare @id int
      declare my_cursor cursor for select autoid from deleted 
      open my_cursor
      fetch next from my_cursor into @id
      while @@fetch_status = 0
      begin
        exec proc_name @id
        fetch next from my_cursor into @id
      end
    endgo
    然后你做个删除试试
      

  17.   

    create proc proc_name
    @id int
    as  declare @temp table (id int)
      
      insert @temp values (@id)
      
      while exists (
        select autoid from tablename
        where fatherid in (select id from @temp)
        and autoid not in (select id from @temp)
      )
      insert @temp 
      select autoid from tablename
      where fatherid in (select id from @temp)
      and autoid not in (select id from @temp)
      
      delete from tablename
      where  autoid in (select id from @temp)go
    create trigger treedelete on tablename 
    after delete  
    as
    begin
      declare @id int
      declare my_cursor cursor for select autoid from deleted 
      open my_cursor
      fetch next from my_cursor into @id
      while @@fetch_status = 0
      begin
        exec proc_name @id
        fetch next from my_cursor into @id
      end
    endgo
      

  18.   

    嵌套触发器的方法:alter database yourdatebasename
       set recursive_triggers on
    go
    drop trigger delete_tree
    go
    create trigger delete_tree on yourtablename after delete
    as
    begin
      if exists (select * from deleted)
      delete from yourtablename from deleted where yourtablename.fatherid=deleted.autoid 
    end
    go
      

  19.   

    create table #temp
    (id int)
    declare @id int
    set @id = 2
    insert into #temp values (@id)
    while exists (select id from tree where fatherid in (select * from #temp) and id not in (select * from #temp) )
       insert into #temp select id from tree where fatherid in (select * from #temp) and id not in (select * from #temp)
    delete tree from #temp where tree.id = #temp.id
    select * from tree