SELECT parent = 表1.名称,
child = 表2.名称
INTO #Temp01
FROM 你的表 AS 表2 LEFT JOIN 你的表 AS 表2
---
--
INSERT #Temp01
SELECT '最高级',
'公司机关'
UNION
SELECT '最高级',
'混凝土分公司'
--
CREATE PROCEDURE expand (@current char(20)) as
SET NOCOUNT ON
DECLARE @level int, @line char(20)
CREATE TABLE #stack (item char(20), level int)
INSERT INTO #stack VALUES (@current, 1)
SELECT @level = 1WHILE @level > 0
BEGIN
   IF EXISTS (SELECT * FROM #stack WHERE level = @level)
      BEGIN
         SELECT @current = item
         FROM #stack
         WHERE level = @level
         SELECT @line = space(@level - 1) + @current
         PRINT @line
         DELETE FROM #stack
         WHERE level = @level
            AND item = @current
         INSERT #stack
            SELECT child, @level + 1
            FROM #Temp01
            WHERE parent = @current
         IF @@ROWCOUNT > 0
            SELECT @level = @level + 1
      END
   ELSE
      SELECT @level = @level - 1
END -- WHILE---
EXEC expand '最高级'

解决方案 »

  1.   

    SELECT parent = 表1.名称,
    child = 表2.名称
    INTO #Temp01
    FROM 你的表 AS 表2 LEFT JOIN 你的表 AS 表1
    ---
    --
    INSERT #Temp01
    SELECT '最高级',
    '公司机关'
    UNION
    SELECT '最高级',
    '混凝土分公司'
    --
    CREATE PROCEDURE expand (@current char(20)) as
    SET NOCOUNT ON
    DECLARE @level int, @line char(20)
    CREATE TABLE #stack (item char(20), level int)
    INSERT INTO #stack VALUES (@current, 1)
    SELECT @level = 1WHILE @level > 0
    BEGIN
       IF EXISTS (SELECT * FROM #stack WHERE level = @level)
          BEGIN
             SELECT @current = item
             FROM #stack
             WHERE level = @level
             SELECT @line = space(@level - 1) + @current
             PRINT @line
             DELETE FROM #stack
             WHERE level = @level
                AND item = @current
             INSERT #stack
                SELECT child, @level + 1
                FROM #Temp01
                WHERE parent = @current
             IF @@ROWCOUNT > 0
                SELECT @level = @level + 1
          END
       ELSE
          SELECT @level = @level - 1
    END -- WHILE---
    EXEC expand '最高级'
      

  2.   

    --示例--示例数据
    create table tb(编号 int,名称 varchar(20),编号_父亲 int)
    insert tb select 2,'公司机关',1
    union all select 3,'党政+人力资源部',2
    union all select 4,'生产计划部',2
    union all select 5,'科研开发部',2
    union all select 6,'证券投资部',2
    union all select 7,'财务部',2
    union all select 8,'混凝土分公司',1
    union all select 9,'办公室',8
    union all select 10,'奎屯分公司',8
    union all select 11,'安全设备部',8
    union all select 12,'巴州分公司',8
    union all select 13,'技术质量部',8
    union all select 14,'材料供应科',8
    union all select 15,'清欠办',8
    union all select 16,'经营部',8
    union all select 17,'财务部',8
    union all select 18,'销售+生产指挥中心',8
    union all select 19,'生产指挥中心',18
    union all select 20,'销售部',18
    union all select 21,'销售一科',20
    union all select 22,'销售二科',20
    union all select 23,'销售三科',20
    union all select 24,'预拌一站',8
    union all select 25,'一线',24
    union all select 26,'二线',24
    union all select 27,'三线',24
    union all select 28,'一班',25
    union all select 29,'二班',25
    union all select 30,'一班',26
    union all select 31,'二班',26
    union all select 32,'一班',27
    union all select 33,'二班',27
    union all select 34,'预拌二站',8
    union all select 35,'四线',34
    union all select 36,'五线',34
    union all select 38,'一班',35
    union all select 39,'二班',35
    union all select 40,'一班',36
    union all select 41,'二班',36
    union all select 44,'科研检测有限公司',1
    union all select 45,'粉煤灰车间',1
    go--处理函数
    create function f_id(
    )returns @re table(id int,level int,sid varchar(8000))
    as
    begin
    declare @l int
    set @l=0
    insert @re select 编号,@l,right(10000+编号,4)
    from tb a
    where not exists(
    select 1 from tb where 编号=a.编号_父亲)
    while @@rowcount>0
    begin
    set @l=@l+1
    insert @re select a.编号,@l,b.sid+right(10000+a.编号,4)
    from tb a,@re b 
    where a.编号_父亲=b.id and b.level=@l-1
    end
    return
    end
    go--调用函数实现查询
    select 名称=space(b.level*4)+a.名称
    from tb a,f_id() b
    where a.编号=b.id
    order by b.sid
    go--删除测试
    drop table tb
    drop function f_id/*--测试结果名称                           
    -------------------------------
    公司机关
        党政+人力资源部
        生产计划部
        科研开发部
        证券投资部
        财务部
    混凝土分公司
        办公室
        奎屯分公司
        安全设备部
        巴州分公司
        技术质量部
        材料供应科
        清欠办
        经营部
        财务部
        销售+生产指挥中心
            生产指挥中心
            销售部
                销售一科
                销售二科
                销售三科
        预拌一站
            一线
                一班
                二班
            二线
                一班
                二班
            三线
                一班
                二班
        预拌二站
            四线
                一班
                二班
            五线
                一班
                二班
    科研检测有限公司
    粉煤灰车间(所影响的行数为 41 行)
    --*/