数据库有一张表,结构如下:ID          NAME            PIDOOOO        LVL1_1          NULL
0001        LVL2_1          0000
0002        LVL2_2          0000
0003        LVL3_1          0001
0004        LVL3_2          0001
0005        LVL3_3          0002要求查询后的结果为:LVL1_ID     LVL1_NAME    LVL2_ID      LVL2_NAME      LVL3_ID      LVL3_NAME0000        LVL1_1       0001         LVL2_1         0003         LVL3_1
0000        LVL1_1       0001         LVL2_1         0004         LVL3_2
0000        LVL1_1       0002         LVL2_2         0005         LVL3_3求高手解答……

解决方案 »

  1.   

    如果有通用的解决办法最好了
    通用是指:
    数据库是以ID,NAME,PID的结构
    结果列名全部以 LVLN_ID,LVLN_NAME的方式命名
      

  2.   

    如果通过程序的话,不是很困难。
    关键是,这是个纯数据库的需求,要求把查出来的结果存到一张表里的。PS:这是本人在BI项目中遇到的实际性难题
    要求生成的表是一张维度表
      

  3.   

    参考这个.
    /*
    标题:查询指定节点及其所有子节点的函数
    作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2008-05-12
    地点:广东深圳
    */create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
    insert into tb values('001' , null  , '广东省')
    insert into tb values('002' , '001' , '广州市')
    insert into tb values('003' , '001' , '深圳市')
    insert into tb values('004' , '002' , '天河区')
    insert into tb values('005' , '003' , '罗湖区')
    insert into tb values('006' , '003' , '福田区')
    insert into tb values('007' , '003' , '宝安区')
    insert into tb values('008' , '007' , '西乡镇')
    insert into tb values('009' , '007' , '龙华镇')
    insert into tb values('010' , '007' , '松岗镇')
    go--查询指定节点及其所有子节点的函数
    create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
    as
    begin
      declare @level int
      set @level = 1
      insert into @t_level select @id , @level
      while @@ROWCOUNT > 0
      begin
        set @level = @level + 1
        insert into @t_level select a.id , @level
        from tb a , @t_Level b
        where a.pid = b.id and b.level = @level - 1
      end
      return
    end
    go--调用函数查询001(广东省)及其所有子节点
    select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    002  001  广州市
    003  001  深圳市
    004  002  天河区
    005  003  罗湖区
    006  003  福田区
    007  003  宝安区
    008  007  西乡镇
    009  007  龙华镇
    010  007  松岗镇(所影响的行数为 10 行)
    */--调用函数查询002(广州市)及其所有子节点
    select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    002  001  广州市
    004  002  天河区(所影响的行数为 2 行)
    */--调用函数查询003(深圳市)及其所有子节点
    select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    003  001  深圳市
    005  003  罗湖区
    006  003  福田区
    007  003  宝安区
    008  007  西乡镇
    009  007  龙华镇
    010  007  松岗镇(所影响的行数为 7 行)
    */drop table tb
    drop function f_cid
      

  4.   

    http://topic.csdn.net/u/20080920/15/424c77bf-7610-4888-be85-9a43e70f55c6.html?4728第16-18楼
      

  5.   


    declare @t table(ID varchar(5) ,        NAME varchar(10),           PID varchar(5))
    insert into @t
      select '0000',        'LVL1_1',          NULL 
    union all select '0001',        'LVL2_1',          '0000' 
    union all select '0002',        'LVL2_2',          '0000' 
    union all select '0003',        'LVL3_1',          '0001' 
    union all select '0004',        'LVL3_2',          '0001' 
    union all select '0005',        'LVL3_3',          '0002'
    union all select '0006',        'LVL3_2_1',         '0004'
    if object_id('tempdb..#') is not null
    drop table #select *
    ,flag = 0
    into #
    from @t
    update a set
    flag = 1
    from # a
    left join # b on a.id = b.pid  
    where b.pid is nulldeclare @lev int
    set @lev = 1while  @@rowcount >0
    begin
    set @lev = @lev+1
    update a set 
    flag = case when @lev > a.flag then @lev else a.flag end
    from # a
    left join  # b on a.id = b.pid
    where b.flag = @lev-1
    endselect * from #declare @sql1 varchar(1000)
    declare @sql2 varchar(1000)
    set @sql1 = 'select * from # a'+rtrim(@lev-1)
    set @sql2 = ' where a'+rtrim(@lev-1)+'.pid is null'
    while @lev>2
    begin
    set @sql1 = @sql1+' left join  # a'+rtrim(@lev-2) +' on a'+rtrim(@lev-1) +'.id = a'+rtrim(@lev-2) +'.pid'
    set @lev = @lev-1
    end
    exec (@sql1+@sql2)
      

  6.   

    ID    NAME       PID   flag        ID    NAME       PID   flag        ID    NAME       PID   flag        ID    NAME       PID   flag        
    ----- ---------- ----- ----------- ----- ---------- ----- ----------- ----- ---------- ----- ----------- ----- ---------- ----- ----------- 
    0000  LVL1_1     NULL  4           0001  LVL2_1     0000  3           0003  LVL3_1     0001  1           NULL  NULL       NULL  NULL
    0000  LVL1_1     NULL  4           0001  LVL2_1     0000  3           0004  LVL3_2     0001  2           0006  LVL3_2_1   0004  1
    0000  LVL1_1     NULL  4           0002  LVL2_2     0000  2           0005  LVL3_3     0002  1           NULL  NULL       NULL  NULL