问题求一条SQL语句
树壮结构:总公司 -> 公司 -> 商店 -> 库房 单位数据库结构为: 单位名称
单位编号
上级单位编号单位数据为 单位名称 总公司
单位编号  01 
上级单位编号 单位名称 公司
单位编号  02 
上级单位编号 01单位名称 商店
单位编号  03 
上级单位编号  02单位名称 库房
单位编号  04 
上级单位编号  03也就是说他们是上下级的关系。有职工  总公司职工   公司职工 商店职工 库房职工职工数据库结构为: 职工名称
单位编号单位数据为 职工名称 总公司职工
单位编号  01 
职工名称 公司职工
单位编号  02 
职工名称 商店职工
单位编号  03 
职工名称 库房职工
单位编号  04 我想点 总公司时他们全出来,
点公司时出来“公司职工 商店职工 库房职工”
点商店时出来“ 商店职工 库房职工”
点库房时出来“ 库房职工”求 点公司时出来“公司职工 商店职工 库房职工”这条SQL语句。
SELECT * FROM 职工 WHERE 单位编号 = 02  OR  ...

解决方案 »

  1.   

    --公司表
    create table t_company(company_name varchar(50),company_no varchar(10),parent_no varchar(10))
    insert into t_company(company_name,company_no,parent_no)
    select '总公司','01',null
    union all select '公司','02','01'
    union all select '商店','03','02'
    union all select '库房','04','03'
    --职员表
    create table t_employee(name varchar(20),company_no varchar(10))
    insert into t_employee(name,company_no)
    select '总公司职工','01'
    union all select '公司职工','02'
    union all select '商店职工','03'
    union all select '库存职工','04'
    --取指定公司的下级公司
    create function f_getchild(@no varchar(10))
    returns @t table(no varchar(10))
    as
    begin
    declare @company_no varchar(10)
    select @company_no = isnull(company_no,'') from t_company where parent_no = @no
    while len(@company_no) > 0 
    begin
    insert into @t(no)
    select company_no from t_company where company_no = @company_no
    if not exists(select 1 from t_company where parent_no = @company_no)
    return
    select @company_no = isnull(company_no,'') from t_company where parent_no = @company_no
    end
    return
    end
    --查询
    select name from t_employee
    where company_no in (select no from dbo.f_getchild('01'))
      

  2.   

    谢谢gahade(沙果) 你给的SQL运行没有错误,可加了我的数据就取不出正确的结果.下面t_company的数据
    company_name,company_no,parent_no单位,0001,2
    部门,0002,2
    公司1,0003,0001
    公司1办公室,0004,2
    工厂1,0005,0001
    工厂办公室,0006,2
    商店,0007,0003select no from dbo.f_getchild('0001')的返回值,我期待是0001,0003,0005,0007
    可是却返回0005 不知道为什么?请您解答一下
    (你的回答是正确的,就是我的数据有问题,等结贴马上给你分)
      

  3.   

    不必讳言,gahade(沙果) 的函数写得有问题,只能处理每层都只有一条记录的情况修改如下:create function f_getchild(@no varchar(10))
    returns @t table(no varchar(10))
    as
    begin
    insert @t
    select company_no from t_company where parent_no = @no
    while exists (
       select 1 from  t_company where company_no not in (select company_no from @t)
          and parent_no in (select company_no from @t)
       )
    insert into @t(no)
    select company_no from t_company where company_no not in (select company_no from @t)
          and parent_no in (select company_no from @t)return
    end
      

  4.   

    pengda1i(冒牌大力 V0.3)   
     
    是正确的我以前写的都是返回变量的,大力写的非常简单明了,学习
      

  5.   

    呵呵.才睡醒!
    不好意思弄错了,修改下用递归实现!
    alter function f_getchild(@no varchar(10))
    returns @t table(no varchar(10))
    as
    begin
    declare @t_temp table(id int identity(1,1),child_no varchar(10))
    insert into @t(no)
    select company_no from t_company where parent_no = @no insert into @t_temp(child_no)
    select company_no from t_company where parent_no = @no declare @child_temp  varchar(10),@max_id  int,@min_id int
    select @max_id = max(id),@min_id = min(id) from @t_temp
    while @min_id <= @max_id
    begin
    select @child_temp = child_no from @t_temp where id = @min_id
    insert into @t(no)
    select * from dbo.f_getchild(@child_temp)
    select @min_id = @min_id + 1
    end
    return
    end
      

  6.   

    谢谢大家,谢谢gahade(沙果) 
    高手!