不想使用多个表格表示,是在.NET中做代码,请高手指教啊!

解决方案 »

  1.   

    --> 测试数据: #T
    if object_id('tempdb.dbo.#T') is not null drop table #T
    create table #T (编码 int,栏目名称 varchar(12),栏目序号 int,上级目录 int)
    insert into #T
    select 10,'电脑知识',10,0 union all
    select 20,'驴行旅游',20,0 union all
    select 30,'我喜欢我推荐',30,0 union all
    select 1010,'─计算机安全',10,10 union all
    select 1020,'─使用技巧',20,10 union all
    select 3010,'─无读不丈夫',10,30 union all
    select 3020,'─电影人生',20,30 union all
    select 101010,'──w2k3专题',10,1010select * from #T order by ltrim(编码)/*
    编码        栏目名称    栏目序号     上级目录
    ----------- ------------ ----------- -----------
    10          电脑知识     10          0
    1010        ─计算机安全  10          10
    101010      ──w2k3专题   10          1010
    1020        ─使用技巧    20          10
    20          驴行旅游     20          0
    30          我喜欢我推荐 30          0
    3010        ─无读不丈夫  10          30
    3020        ─电影人生    20          30
    */
      

  2.   

    SELECT [编码], [栏目名称],[栏目序号], [上级目录] FROM @T ORDER BY  [编码],[栏目序号], [上级目录]
      

  3.   

    呵呵,那个编码我自己输的时候,是“分层输入”的,客户可能不这样,所以,不能只根据编码排序啊,比如,他可能不输入1010,而输入11,或者,78
    而且,栏目序号只是一个设计用来表示级别内的顺序号,也不能作为整个的排序参考的
    谢谢hery200 和limpire
      

  4.   

    http://topic.csdn.net/u/20080528/15/08b5af34-f87c-4757-8672-a4e7b29f1080.html
      

  5.   

    参考下面:/******************************************************************************/
    /*回复:20080528002总:00054                                                   */
    /*主题:树的排序                                                                          */
    /*作者:二等草                                                                            */
    /******************************************************************************/set nocount on--数据--------------------------------------------------------------------------
    create table Ta (DeptID int,DeptName varchar(16),ParentID int,CorpID int)
    insert into Ta
    select 1,'客服部',0,10001 union all
    select 2,'市场部',0,10002 union all
    select 3,'开发部',0,10001 union all
    select 4,'财务部',0,10002 union all
    select 5,'客服1部',1,10001 union all
    select 6,'客服2部',1,10001 union all
    select 7,'市场1部',2,10002 union all
    select 8,'市场2部',2,10002 union all
    select 9,'开发1部',3,10001 union all
    select 10,'开发2部',3,10001 union all
    select 11,'开发1部1组',9,10001 union all
    select 12,'开发1部2组',9,10001 union all
    select 13,'开发1部1组Java',11,10001 union all
    select 14,'开发1部1组Dotnet',11,10001;
    go
    --代码--------------------------------------------------------------------------
    create function getpath(@id int,@f int)
    returns varchar(100)
    as
    begin
     declare @s varchar(8000),@i int
     while exists(select 1 from ta where deptid = @id)
       select @i = isnull(@i,0)+1,@id = parentid,@s = rtrim(deptid)+deptname+isnull('-'+@s,'') from ta where  deptid = @id
     if @f = 0 return @s
     return rtrim(@i-1)
    end
    go
    select deptname,depth=cast(dbo.getpath(deptid,1) as int),corpid from ta where corpid =10001 order by dbo.getpath(deptid,0)
    go/*结果--------------------------------------------------------------------------
    deptname         depth       corpid      
    ---------------- ----------- ----------- 
    客服部              0           10001
    客服1部             1           10001
    客服2部             1           10001
    开发部              0           10001
    开发2部             1           10001
    开发1部             1           10001
    开发1部1组           2           10001
    开发1部1组Java       3           10001
    开发1部1组Dotnet     3           10001
    开发1部2组           2           10001
    --清除------------------------------------------------------------------------*/
    go
    drop function getpath
    go
    drop table ta
      

  6.   


    --> --> (Ben)生成測試數據
     
    if not object_id('Tempdb..#T') is null
    drop table #T
    Go
    Create table #T([id] int,[name] nvarchar(8),[num] int,[parent] int)
    Insert #T
    select 10,'电脑知识',10,0 union all
    select 20,'驴行旅游',20,0 union all
    select 30,'我喜欢我推荐',30,0 union all
    select 1010,'─计算机安全',10,10 union all
    select 1020,'─使用技巧',20,10 union all
    select 3010,'─无读不丈夫',10,30 union all
    select 3020,'─电影人生',20,30 union all
    select 101010,'──w2k3专题',10,1010
    Gowith tb as
    (
    select typeid=row_number()over(order by id),* from #T a where a.parent=0 
    union all
    select typeid=tb.typeid*10+row_number()over(order by a.id), a.* from #T a,tb where a.parent=tb.id 
    )
    select * from tb order by cast(typeid as varchar)
      

  7.   

    --> 测试数据: #T
    if object_id('tempdb.dbo.#T') is not null drop table #T
    create table #T (编码 int,栏目名称 varchar(12),栏目序号 int,上级目录 int)
    insert into #T
    select 10,'电脑知识',10,0 union all
    select 20,'驴行旅游',20,0 union all
    select 30,'我喜欢我推荐',30,0 union all
    select 1010,'─计算机安全',10,10 union all
    select 1020,'─使用技巧',20,10 union all
    select 3010,'─无读不丈夫',10,30 union all
    select 3020,'─电影人生',20,30 union all
    select 101010,'──w2k3专题',10,1010;/*
    呵呵,那个编码我自己输的时候,是“分层输入”的,客户可能不这样,所以,不能只根据编码排序啊,比如,他可能不输入1010,而输入11,或者,78
    而且,栏目序号只是一个设计用来表示级别内的顺序号,也不能作为整个的排序参考的 
    ----------------------
    那就自己构造一个分层的编号:ID
    */with T as
    (
        select ID=convert(varchar(1000),right('0000'+ltrim(row_number()over(order by 编码)),5)),* from #T where 上级目录=0
        union all
        select convert(varchar(1000),b.ID+right('0000'+ltrim(row_number()over(order by a.编码)),5)),a.* from #T a join T b on a.上级目录=b.编码
    )
    select * from T order by ID/*
    ID              编码        栏目名称     栏目序号    上级目录
    --------------- ----------- ------------ ----------- -----------
    00001           10          电脑知识     10          0
    0000100001      1010        ─计算机安全 10          10
    000010000100001 101010      ──w2k3专题 10          1010
    0000100002      1020        ─使用技巧   20          10
    00002           20          驴行旅游     20          0
    00003           30          我喜欢我推荐 30          0
    0000300001      3010        ─无读不丈夫 10          30
    0000300002      3020        ─电影人生   20          30
    */