有一个层次结构的表,现在要求利用他的外部编码和父子关系生成内部编码,内部编码四位一级
表1.
编码               父编码
1
1.1                1
1.1.1                1.1
1.1.3                1.1
2                 
2.1                 2
3                
4                  
结果
编码               父编码    内部编码
1                       0001
1.1                 1            00010001
1.1.1                 1.1   000100010001
1.1.3                 1.1   000100010002
2                       0002
2.1                  2             00020001
3                        0003
4                      0004

注意这个
1.1.1                 1.1   000100010001
1.1.3                 1.1   000100010002
如果是第二个儿子的话,按照顺序来写,第一个是0001第二个是0002希望求个用SQL2000解决的!SQL2005的不要,谢谢了

解决方案 »

  1.   

    /*
    Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)   Jul  9 2008 14:43:34   Copyright (c) 
    1988-2008 Microsoft Corporation  Enterprise Evaluation Edition on Windows NT 5.1 <X86> 
    (Build 2600: Service Pack 3) 
     愿和大家共同进步
    如有雷同、实属巧合
    ●●●●●2009-09-03 17:47:36.077●●●●●
     ★★★★★soft_wsx★★★★★
    */
    --树型结构处理之双编号(广度深度排序)
    if OBJECTPROPERTY(object_id('tb'),'isusertable')<>0 
      drop table tb
    create table tb(ybh nvarchar(10),ebh nvarchar(10),beizhu nvarchar(1000))
    insert tb
    select '0001',null,'云南省'
    union all select '0002','0001','昆明市'
    union all select '0003','0001','昭通市'
    union all select '0009','0001','大理市'
    union all select '0008',null,'四川省'
    union all select '0004',null,'贵州省'
    union all select '0005','0002','五华区'
    union all select '0007','0002','水富县'
    union all select '0006','0005','西园路192号'
    union all select '0010','0006','金色梧桐'
    union all select '0011','0010','科技有限公司'
    union all select '0015','0007','两碗乡'
    union all select '0013','0015','两碗村'
    union all select '0012','0013','某跨国集团董事长'
    union all select '0014','0008','成都市'--select * from tb
    --广度排序(先显示第一层节点,再显示第二次节点......)
    --定义辅助表
    declare @level_tb table(bh nvarchar(10),level int)
    declare @level int
    set @level=0
    insert @level_tb(bh,level)
    select ybh,@level from tb where ebh is null
    while @@ROWCOUNT>0
      begin
        set @level=@level+1
        insert @level_tb(bh,level)
          select ybh,@level 
            from tb a,@level_tb b
            where a.ebh=b.bh 
                  and b.level=@level-1
      end 
      select a.*,b.* from tb a,@level_tb b where a.ybh=b.bh order by level
    /*
    ybh ebh beizhu bh level
    0001 NULL 云南省 0001 0
    0008 NULL 四川省 0008 0
    0004 NULL 贵州省 0004 0
    0002 0001 昆明市 0002 1
    0003 0001 昭通市 0003 1
    0009 0001 大理市 0009 1
    0014 0008 成都市 0014 1
    0005 0002 五华区 0005 2
    0007 0002 水富县 0007 2
    0006 0005 西园路192号 0006 3
    0015 0007 两碗乡 0015 3
    0010 0006 金色梧桐 0010 4
    0013 0015 两碗村 0013 4
    0011 0010 科技有限公司 0011 5
    0012 0013 某跨国集团董事长 0012 5
    */
      
      --深度排序(模拟单编码法)
       declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
      declare @level int
      set @level=0
      insert @level_tt(ybh,ebh,level)
      select ybh,ybh,@level from tb where ebh is null
      while @@ROWCOUNT>0
      begin 
              set @level=@level+1
              insert @level_tt(ybh,ebh,level)
              select a.ybh,b.ebh+a.ybh,@level
                from tb a,@level_tt b
                where a.ebh=b.ybh and b.level=@level-1
     end
    select space(b.level*2)+'----'+a.beizhu,a.*,b.*
      from tb a,@level_tt b
      where a.ybh=b.ybh
      order by b.ebh
    /*(无列名) ybh ebh beizhu ybh ebh level
    ----云南省 0001 NULL 云南省 0001 0001 0
      ----昆明市 0002 0001 昆明市 0002 00010002 1
        ----五华区 0005 0002 五华区 0005 000100020005 2
          ----西园路192号 0006 0005 西园路192号 0006 0001000200050006 3
            ----金色梧桐 0010 0006 金色梧桐 0010 00010002000500060010 4
              ----科技有限公司 0011 0010 科技有限公司 0011 000100020005000600100011 5
        ----水富县 0007 0002 水富县 0007 000100020007 2
          ----两碗乡 0015 0007 两碗乡 0015 0001000200070015 3
            ----两碗村 0013 0015 两碗村 0013 00010002000700150013 4
              ----某跨国集团董事长 0012 0013 某跨国集团董事长 0012 000100020007001500130012 5
      ----昭通市 0003 0001 昭通市 0003 00010003 1
      ----大理市 0009 0001 大理市 0009 00010009 1
    ----贵州省 0004 NULL 贵州省 0004 0004 0
    ----四川省 0008 NULL 四川省 0008 0008 0
      ----成都市 0014 0008 成都市 0014 00080014 1
      */
      
      
      
      --查找子节点(包括本身节点和子节点)
     declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
      declare @level int
      set @level=0
      insert @level_tt(ybh,ebh,level)
      select ybh,ybh,@level from tb where ybh='0005'
      while @@ROWCOUNT>0
      begin 
              set @level=@level+1
              insert @level_tt(ybh,ebh,level)
              select a.ybh,b.ebh+a.ybh,@level
                from tb a,@level_tt b
                where a.ebh=b.ybh and b.level=@level-1
     end
    select space(b.level*2)+'----'+a.beizhu,a.*,b.*
      from tb a,@level_tt b
      where a.ybh=b.ybh
      order by b.ebh
      
     /*
     (无列名) ybh ebh beizhu ybh ebh level
    ----五华区 0005 0002 五华区 0005 0005 0
      ----西园路192号 0006 0005 西园路192号 0006 00050006 1
        ----金色梧桐 0010 0006 金色梧桐 0010 000500060010 2
          ----科技有限公司 0011 0010 科技有限公司 0011 0005000600100011 3
          */
      
      --查的父节点(包括本身节点和所有的你节点)
     declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
      declare @level int
      set @level=0
      insert @level_tt(ybh,ebh,level)
      select ybh,ebh,@level from tb where ebh='0005'
      while @@ROWCOUNT>0
      begin 
              set @level=@level+1
              insert @level_tt(ybh,ebh,level)
              select a.ebh,b.ebh+a.ebh,@level
                from tb a,@level_tt b
                where a.ybh=b.ybh and b.level=@level-1
     end
    select space(b.level*2)+'----'+a.beizhu,a.*,b.*
      from tb a,@level_tt b
      where a.ybh=b.ybh
      order by b.ebh desc
     
     /*
     (无列名) ybh ebh beizhu ybh ebh level
          ----云南省 0001 NULL 云南省 0001 0005000500020001 3
        ----昆明市 0002 0001 昆明市 0002 000500050002 2
      ----五华区 0005 0002 五华区 0005 00050005 1
    ----西园路192号 0006 0005 西园路192号 0006 0005 0
    */
      本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/soft_wsx/archive/2009/09/04/4521091.aspx改一下就可以了
      

  2.   

    BOM
    CREATE TABLE BOM(PID INT,ID INT)
    INSERT INTO BOM SELECT 801,101
    INSERT INTO BOM SELECT 801,102
    INSERT INTO BOM SELECT 801,103
    INSERT INTO BOM SELECT 801,601
    INSERT INTO BOM SELECT 601,101
    INSERT INTO BOM SELECT 601,105
    INSERT INTO BOM SELECT 601,501
    INSERT INTO BOM SELECT 501,106
    INSERT INTO BOM SELECT 501,121
    GOCREATE FUNCTION F_GETROOT(@PID INT)
    RETURNS INT
    AS
    BEGIN
        DECLARE @ID INT
        WHILE EXISTS(SELECT 1 FROM BOM WHERE ID=@PID)
        BEGIN
            SET @ID=@PID
            SELECT @PID=PID FROM BOM WHERE ID=@ID
        END
        RETURN @PID
    END
    GOSELECT PID=DBO.F_GETROOT(PID),ID FROM BOM
    GO/*
    PID         ID
    ----------- ----------- 
    801         101
    801         102
    801         103
    801         601
    801         101
    801         105
    801         501
    801         106
    801         121
    */
    DROP FUNCTION F_GETROOT
    DROP TABLE BOM
    GO
    --生成测试数据
    create table BOM_1(Item int,bom_head varchar(20),bom_child varchar(20),number int,products_attribute  varchar(20))
    insert into BOM_1 select 1 ,'A' ,'A1',1,'采购'
    insert into BOM_1 select 2 ,'A' ,'A2',2,'生产'
    insert into BOM_1 select 3 ,'A2','A3',3,'生产'
    insert into BOM_1 select 4 ,'A2','A4',2,'采购'
    insert into BOM_1 select 5 ,'A3','A5',2,'采购'
    insert into BOM_1 select 6 ,'A3','A6',1,'采购'
    insert into BOM_1 select 7 ,'B' ,'B1',1,'采购'
    insert into BOM_1 select 8 ,'B' ,'B2',2,'生产'
    insert into BOM_1 select 9 ,'B2','B3',3,'生产'
    insert into BOM_1 select 10,'B2','B4',2,'采购'
    insert into BOM_1 select 11,'B3','B5',2,'采购'
    insert into BOM_1 select 12,'B3','B6',2,'采购'
    go
       --创建用户定义函数,用于取每个父节点下子节点的采购配置信息
    create function f_stock(@bom_head varchar(20))
    returns @t table(bom varchar(20),number int)
    as
    begin 
        declare @level int
        declare @a table(bom varchar(20),number int,products_attribute varchar(20),[level] int)
        set @level=1    if exists(select 1 from BOM_1 where bom_head=@bom_head)    
        insert into @a 
        select bom_child,number,products_attribute,@level 
        from BOM_1 
        where bom_head=@bom_head
        
        while exists(select 1 from @a where [level]=@level and products_attribute='生产')
        begin
            set @level=@level+1
            insert into @a(bom,number,products_attribute,[level])
            select a.bom_child,a.number,a.products_attribute,@level 
            from BOM_1 a,@a b
            where a.bom_head=b.bom and b.[level]=@level-1
        end
        
        insert into @t(bom,number) select bom,number from @a where products_attribute='采购'
        return
    end
    go
    --执行调用,取父节点'A'一个标准配置分解的采购信息及数量
    select * from dbo.f_stock('A')
    --生成测试数据
    create table BOM(ID INT,PID INT,MSG VARCHAR(1000))
    insert into BOM select 1,0,NULL
    insert into BOM select 2,1,NULL
    insert into BOM select 3,1,NULL
    insert into BOM select 4,2,NULL
    insert into BOM select 5,3,NULL
    insert into BOM select 6,5,NULL
    insert into BOM select 7,6,NULL
    go--创建用户定义函数用于取每个父节点下子节点的采购配置信息
    create function f_getChild(@ID VARCHAR(10))
    returns @t table(ID VARCHAR(10),PID VARCHAR(10),Level INT)
    as
    begin
        declare @i int
        set @i = 1
        insert into @t select ID,PID,@i from BOM where PID = @ID
        
        while @@rowcount<>0
        begin
            set @i = @i + 1
            
            insert into @t 
            select 
                a.ID,a.PID,@i 
            from 
                BOM a,@t b 
            where 
                a.PID=b.ID and b.Level = @i-1
        end
        return
    end
    go--执行查询
    select ID from dbo.f_getChild(3)
    go--输出结果
    /*
    ID
    ----
    5
    6
    7
    */--删除测试数据
    drop function f_getChild
    drop table BOM创建用户定义函数,每个子节点de父节点的信息
    --生成测试数据
    create table BOM(ID int,parentID int,sClassName varchar(10))
    insert into BOM values(1,0,'1111'      )
    insert into BOM values(2,1,'1111_1'    )
    insert into BOM values(3,2,'1111-1-1'  )
    insert into BOM values(4,3,'1111-1-1-1') 
    insert into BOM values(5,1,'1111-2'    )go--创建用户定义函数,每个子节点de父节点的信息
    create function f_getParent(@ID int)
    returns varchar(40)
    as
    begin
        declare @ret varchar(40)    while exists(select 1 from BOM where ID=@ID and parentID<>0)
        begin
            select @ID=b.ID,@ret=','+rtrim(b.ID)+isnull(@ret,'')
            from
                BOM a,BOM b
            where
                a.ID=@ID and b.ID=a.parentID
        end
        
        set @ret=stuff(@ret,1,1,'')
        return @ret
    end
    go--执行查询
    select ID,isnull(dbo.f_getParent(ID),'') as parentID from BOM
    go--输出结果
    /*
    ID          parentID                                 
    ----------- ---------------------------------------- 
    1           
    2           1
    3           1,2
    4           1,2,3
    5           1   
    */--删除测试数据
    drop function f_getParent
    drop table BOM
    go
      

  3.   

    fredrickhu
    我不是要那种单纯的父子排序,
    主要问题是要在同级别的子集中分出次序来
      

  4.   

    --测试数据   深度排序     
      DECLARE   @t   TABLE(ID   char(3),PID   char(3),Name   nvarchar(10))   
      INSERT   @t   SELECT   '001',NULL   ,'山东省'   
      UNION   ALL   SELECT   '002','001','烟台市'   
      UNION   ALL   SELECT   '004','002','招远市'   
      UNION   ALL   SELECT   '003','001','青岛市'   
      UNION   ALL   SELECT   '005',NULL   ,'四会市'   
      UNION   ALL   SELECT   '006','005','清远市'   
      UNION   ALL   SELECT   '007','006','小分市'   
        
      --深度排序显示处理   
      --生成每个节点的编码累计(相同当单编号法的编码)   
      DECLARE   @t_Level   TABLE(ID   char(3),Level   int,Sort   varchar(8000))   
      DECLARE   @Level   int   
      SET   @Level=0   
      INSERT   @t_Level   SELECT   ID,@Level,ID   
      FROM   @t   
      WHERE   PID   IS   NULL   
      WHILE   @@ROWCOUNT>0   
      BEGIN   
      SET   @Level=@Level+1   
      INSERT   @t_Level   SELECT   a.ID,@Level,b.Sort+a.ID   
      FROM   @t   a,@t_Level   b   
      WHERE   a.PID=b.ID   
      AND   b.Level=@Level-1   
      END   
        
      --显示结果   
      SELECT   a.*   
      FROM   @t   a,@t_Level   b   
      WHERE   a.ID=b.ID   
      ORDER   BY   b.Sort   
      /*--结果   
      ID       PID       Name                 
      ------   ---------   ----------     
      001     NULL   山东省   
      002     001       烟台市   
      004     002       招远市   
      003     001       青岛市   
      005     NULL   四会市   
      006     005       清远市   
      007     006       小分市   
      --*/--查询指定节点及其所有子节点的函数   
      CREATE   FUNCTION   f_Cid(@ID   char(3))   
      RETURNS   @t_Level   TABLE(ID   char(3),Level   int)   
      AS   
      BEGIN   
      DECLARE   @Level   int   
      SET   @Level=1   
      INSERT   @t_Level   SELECT   @ID,@Level   
      WHILE   @@ROWCOUNT>0   
      BEGIN   
      SET   @Level=@Level+1   
      INSERT   @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   
        
      --调用函数查询002及其所有子节点   
      SELECT   a.*   
      FROM   tb   a,f_Cid('002')   b   
      WHERE   a.ID=b.ID   
      /*--结果   
      ID       PID     Name                 
      ------   -------   ----------     
      002     001     烟台市   
      004     002     招远市   
      --*/ --测试数据
    DECLARE @t TABLE(ID char(3),PID char(3),Name nvarchar(10))
    INSERT @t SELECT '001',NULL ,'山东省'
    UNION ALL SELECT '002','001','烟台市'
    UNION ALL SELECT '004','002','招远市'
    UNION ALL SELECT '003','001','青岛市'
    UNION ALL SELECT '005',NULL ,'四会市'
    UNION ALL SELECT '006','005','清远市'
    UNION ALL SELECT '007','006','小分市'--深度排序显示处理
    --生成每个节点的编码累计(相同当单编号法的编码)
    DECLARE @t_Level TABLE(ID char(3),Level int,Sort varchar(8000))
    DECLARE @Level int
    SET @Level=0
    INSERT @t_Level SELECT ID,@Level,ID
    FROM @t
    WHERE PID IS NULL
    WHILE @@ROWCOUNT>0
    BEGIN
        SET @Level=@Level+1
        INSERT @t_Level SELECT a.ID,@Level,b.Sort+a.ID
        FROM @t a,@t_Level b
        WHERE a.PID=b.ID
            AND b.Level=@Level-1
    END--显示结果
    SELECT SPACE(b.Level*2)+'|--'+a.Name
    FROM @t a,@t_Level b
    WHERE a.ID=b.ID
    ORDER BY b.Sort
    /*--结果
    |--山东省
      |--烟台市
        |--招远市
      |--青岛市
    |--四会市
      |--清远市
        |--小分市
    --*/
      

  5.   

    create function wsp()
    returns @t table(编码 varchar(10),父编码 varchar(10),px int,内部编码 varchar(20),level int)
    as
    begin
    declare @lev int
    set @lev=1
    insert into @t select *,right('0000'+编码,4),@lev from 
    (select *,px=isnull(nullif((select count(1) from 表1 where 父编码=a.父编码 and 编码<=a.编码),0),1) from 表1 a)a where isnull(父编码,'')=''
    while(@@rowcount>0)
    begin
    set @lev=@lev+1
    insert into @t select a.*,b.内部编码+right('0000'+ltrim(a.px),4),@lev from 
    (select *,px=isnull(nullif((select count(1) from 表1 where 父编码=a.父编码 and 编码<=a.编码),0),1) from 表1 a)a,@t b
    where a.父编码=b.编码 and @lev=b.level+1
    end
    return
    end
    goselect 编码,父编码,内部编码 from dbo.wsp() order by 编码--结果:
    编码         父编码        内部编码
    ---------- ---------- --------------------
    1          NULL       0001
    1.1        1          00010001
    1.1.1      1.1        000100010001
    1.1.3      1.1        000100010002
    2          NULL       0002
    2.1        2          00020001
    3          NULL       0003
    4          NULL       0004
      

  6.   


    --改下,有个地方多余了。create function wsp()
    returns @t table(编码 varchar(10),父编码 varchar(10),内部编码 varchar(20),level int)
    as
    begin
    declare @lev int
    set @lev=1
    insert into @t select *,right('0000'+编码,4),@lev from   表1 where isnull(父编码,'')=''
    while(@@rowcount>0)
    begin
    set @lev=@lev+1
    insert into @t select a.编码,a.父编码,b.内部编码+right('0000'+ltrim(a.px),4),@lev from 
    (select *,px=(select count(1) from 表1 where 父编码=a.父编码 and 编码<=a.编码)from 表1 a)a,@t b
    where a.父编码=b.编码 and @lev=b.level+1
    end
    return
    end
    goselect 编码,父编码,内部编码 from dbo.wsp() order by 编码