code    name           supercode
        01 大部门
0101 二级部门 01
010101 三级部门         0101
0102 二级部门 01
获得一个部门下的所有子部门           如:01

解决方案 »

  1.   

    Select  name as 子部门 from TableName where code like '01%'
      

  2.   


    select name 子部门 from 表 where left(code,2)='01'
      

  3.   

        DECLARE @code int
        SELECT * FROM #t WHERE supercode=like 'LEFT(code,Len(code)-2)%' AND code=@code)
      

  4.   

    select  name as 子部门 from TableName where supercode=(select code from TableName where name='大部门')
      

  5.   

    create table test 
    (
    code varchar(10),
    name varchar(20),
    supercode varchar(10)
    )insert into testselect '01','大部门',' '
    union 
    select '0101','二级部门','01'
    union
    select '010101','三级部门','0101'
    union 
    select '0102','二级部门','01'
    union
    select '010102','三级部门','0101'
    union 
    select '010103','三级部门','0101'create  proc usp_text 
    @dept varchar(10)as  
    set nocount on  declare @s varchar(8000)
    set @s=''
    set @s ='select name as 子部门 from test
      where left(supercode,len('+''''+@dept+''''+'))'+'='+@dept  exec (@s)
    returnexec usp_text '0101'子部门                  
    -------------------- 
    三级部门
    三级部门
    二级部门drop table test 
    drop proc usp_text
      

  6.   

    exec usp_text '0101'
    这里的'0101'更改为你需要查找的!
      

  7.   

    select * from tbl where pid not in (select id from tbl); ---所有根节点
    select * from tbl where id not in (select pid from tbl); ---所有叶子节点
    select * from tbl where id in (select pid from tbl); ---所有父节点
      

  8.   

    再帮帮忙  regithanhu 的方法是对的 我想再加个条件  or code='0101'
    想要的结果是  010101,010102
      

  9.   

    select name from '表名' where supercode=(select name from '表名')