SQL掌握的不好,现向大家请教3个存储过程,需求如下:问题一:当删除一级栏目时,如果该一级栏目下有二级栏目并且二级栏目下有三级栏目,则先删除三级栏目,再删除二级栏目,最后删除一级栏目;如果二级栏目下没有三级栏目,则先删除二级栏目,再删除一级栏目;如果该一级栏目下没有任何栏目,则直接删除。同时删除另一个信息表中所属该一级栏目的全部记录问题二:当删除二级栏目时,如果该二级栏目下有三级栏目,则先删除三级栏目,再删除二级栏目;如果没有,则直接删除该二级栏目。同时删除另一个信息表中所属该二级栏目的全部记录问题三:删除三级栏目的同时删除另一个信息表中所属三级栏目的记录。表结构和字段如下:Category表
CategoryID    int   identity(1,1)   primary   key,        --编号 
CategoryName   varchar(20)   not   null,                  --名称 
ParentID   int,                                           --父类ID 
LevelID   int   default   0,                              --类级别 0-一级栏目 1-二级 2-三级 
IsDeleted   int   default   0                             --是否被删除     0-没有删除       1-已经删除 Content表
ContentID int  identity(1,1)   primary   key,        --编号 
Title varchar(100) not null,                         --标题
PID int,                                             --一级栏目ID
CID int,                                             --二级栏目ID(无二级栏目,则为0)
SID int,                                             --三级栏目ID(无三级栏目,则为0)
IsDeleted   int   default   0                        --是否被删除     0-没有删除       1-已经删除 存储过程的参数为categoryid请大家帮帮忙,谢谢。

解决方案 »

  1.   

    Category表 
    CategoryID    int   identity(1,1)   primary   key,        --编号  
    CategoryName   varchar(20)   not   null,                  --名称  
    ParentID   int,                                           --父类ID  
    LevelID   int   default   0,                              --类级别 0-一级栏目 1-二级 2-三级  
    IsDeleted   int   default   0                             --是否被删除     0-没有删除       1-已经删除  Content表 
    ContentID int  identity(1,1)   primary   key,        --编号  
    Title varchar(100) not null,                         --标题 
    PID int,                                             --一级栏目ID 
    CID int,                                             --二级栏目ID(无二级栏目,则为0) 
    SID int,                                             --三级栏目ID(无三级栏目,则为0) 
    IsDeleted   int   default   0                        --是否被删除     0-没有删除       1-已经删除  存储过程的参数为categoryid 
    create proc test_p(@categoryid int)
    as
    declare @Category table( CategoryID    int  ,  ParentID   int,   LevelID   int)
    insert @Category
    select CategoryID,ParentID,LevelID from Category where CategoryID=@CategoryID
    if @@rowcount>0
    begin
    insert @Category
    select 
    t.CategoryID,t.ParentID,t.LevelID 
    from 
    Category t
    join
    @Category t1 on t.ParentID=t1.CategoryID
    where not exists(select 1 from @Category where CategoryID=t.CategoryID)
    enddelete t from Category t join @Category t1 on t.CategoryID=t1.CategoryIDif exists(select 1 from @Category where  LevelID=2)--有三级栏目
     delete Content.............
      

  2.   

    你的表设计的有问题,
    CID int,                                             --二级栏目ID(无二级栏目,则为0) 
    SID int,                                             --三级栏目ID(无三级栏目,则为0)
    他们只能有一个二级栏目和三级栏目吗!
    类型应当用nvarchar吧,还是你还有一个表来存储二级栏目Id
      

  3.   

    create proc [dbo].[Category_delete] 
    (
    @categoryid int

    as
    declare @levelid int
    select @levelid=
    (
        select 
     LevelID 
    from 
    Category
    where 
    CategoryID=@categoryid
    )
    if @levelid=0
    begin
    update 
    Category

    set 
    IsDeleted=1
    where
                CategoryID=@categoryid
            update 
    [Content]
    set 
    IsDeleted=1
    where
                ContentID=@categoryid
    and CID is not null
    and SID is not null
    end
    else if @levelid=1
        
    begin
    update 
    Category

    set 
    IsDeleted=1
    where
               CategoryID=@categoryid
            update 
    [Content]

    set 
    IsDeleted=1
    where
               ContentID=@categoryid
    and SID is not null
    end
    else 
       begin
    update 
    Category
    set 
    IsDeleted=1
    where
               CategoryID=@categoryid
            update 
    [Content]
    set 
    IsDeleted=1
    where
               ContentID=@categoryid
    end
      

  4.   

    qiuming0306 老兄的方案不错
    顶!!!!
      

  5.   

    建议楼主去阅读一下尚学堂的《BBS视频教程2007》和《JDBC_MySQL_BBS视频教程》,这个问题讲的非常清楚
    而且还有高效率树状结构设计