slect 学生
from tb 
where where 校长=@校长

解决方案 »

  1.   

    select *
    from tb 
    where where 校长='李校长'
      

  2.   

    declare @str varchar
    set @str='李校长'
    select * from tb where where 校长=@str
      

  3.   

    重新修正一下
    一个一级主管对应多个二级主管和成员,一个二级主管对应多个三级主管和成员,三级主管对应多个成员...依次类推
    一级主管直接管理二级主管,间接管理三级主管
    现要查出一级主管管理的所有人
    属于SQL的递归查询,现想用存储过程实现
      

  4.   

    重新修正一下 
    一个一级主管对应多个二级主管和成员,一个二级主管对应多个三级主管和成员,三级主管对应多个成员...依次类推 
    一级主管直接管理二级主管,间接管理三级主管 
    现要查出一级主管管理的所有人 
    属于SQL的递归查询,现想用存储过程实现
    SQL2000数据库
      

  5.   


    SQL2005可以直接用递归查询
      

  6.   


    create table Tree(nId smallint, NodeName nvarchar(16), pId smallint)
    insert Tree select 1, '中国', NULL
    union all select 2, '北京', 1
    union all select 3, '上海', 1
    union all select 4, '湖北', 1
    union all select 5, '湖南', 1
    union all select 6, '武汉', 4
    union all select 7, '孝感', 4
    union all select 8, '长沙', 5
    union all select 9, '岳阳', 5
    union all select 10, '海淀区', 2
    union all select 11, '朝阳区', 2
    union all select 12, '上地', 10
    union all select 13, '西三旗', 10go
    create procedure sp_GetNoedsByRootID
        @nId smallint
    as
    begin    
        with NewTree(nId, NodeName, pId) as
        (
            select * from Tree where nId=@nId
            union all
            select T.* from Tree T join NewTree NT on T.pId=NT.nId
        )
        select NT.nId, NT.NodeName, PT.NodeName as [ParentName]
        from NewTree NT join Tree PT on NT.pId=PT.nId            
    end
    gocreate procedure sp_DelNoedsByRootID
        @nId smallint
    as
    begin
        declare @sql nvarchar(max)
        set @sql='
                    with NewTree(nId, NodeName, pId) as
                    (
                        select * from Tree where nId=@nId
                        union all
                        select T.* from Tree T join NewTree NT on T.pId=NT.nId
                    )
                    delete Tree 
                    from Tree T join NewTree NT on T.nId=NT.nId
                '
        exec sp_executesql @sql, N'@nId smallint', @nId
    end
    go--查找节点及子节点
    exec sp_GetNoedsByRootID 2
    /*
    nId    NodeName         ParentName
    ------ ---------------- ----------------
    2      北京               中国
    10     海淀区              北京
    11     朝阳区              北京
    12     上地               海淀区
    13     西三旗              海淀区(5 行受影响)
    */--删除节点及子节点
    exec sp_DelNoedsByRootID 2
    select * from Tree
    /*
    nId    NodeName         pId
    ------ ---------------- ------
    1      中国               NULL
    3      上海               1
    4      湖北               1
    5      湖南               1
    6      武汉               4
    7      孝感               4
    8      长沙               5
    9      岳阳               5(8 行受影响)
    */drop table Tree
    drop procedure sp_GetNoedsByRootID, sp_DelNoedsByRootID
      

  7.   


    create table CompanyMember( EmployeeID int, ManagerID int , EmpName char(10), [level]int)
    insert into CompanyMember
    select 1, NULL,'Nancy', 1 union all   
    select 2, 1, 'Andrew', 2 union all
    select 3, 1, 'Janet'  , 2 union all
    select 4, 1, 'Margaret', 2 union all
    select 5, 2, 'Steven', 3 union all
    select 6, 2, 'Michael', 3 union all
    select 7, 3, 'Robert', 3 union all
    select 8, 3, 'Laura', 3 union all
    select 9, 3, 'Ann', 3 union all
    select 10, 4, 'Ina', 3 
    ---------------------------------------------------------
    create procedure GetCompanyRelation @level smallint
    as
    begin
    With TableEmployee (EmployeeID , ManagerID  , EmpName , [level],ManagerName)
    --EmployeeID :所有员工(包括主管)ID,ManagerID  :主管ID,EmpName :员工名,ManagerName:主管名
    --[level]:所在的领导层
    as
    (
    select EmployeeID , ManagerID  , EmpName , [level] ,(case when [level]=1 then NULL else EmpName end) as ManagerName
        from CompanyMember
        where [level]=1
        union all
        select C.EmployeeID , C.ManagerID  ,C. EmpName , C.[level],T. EmpName as ManagerName
        from CompanyMember C
    join  TableEmployee T
        on T.EmployeeID=C.ManagerID
    )
    select * from TableEmployee
    end
    -------------------------------------------------------------
    exec GetCompanyRelation 1--求第一层下所有人员信息
    -------------------------------------------------------
    EmployeeID ManagerID EmpName level ManagerName
    --------------------------------------------------------------
    1          NULL Nancy      1          NULL
    2 1 Andrew     2          Nancy     
    3 1 Janet      2          Nancy     
    4 1 Margaret   2          Nancy     
    10 4 Ina        3          Margaret  
    7 3 Robert     3          Janet     
    8 3 Laura      3          Janet     
    9 3 Ann        3          Janet     
    5 2 Steven     3          Andrew    
    6 2 Michael    3          Andrew    
      

  8.   


    来一个简单明了的
    if object_id('tb')>0 drop table tb
    create table tb(id int ,sname nvarchar(10),parentid int)insert tb  values (1,'abc1',0)
    insert tb  values(2,'abc2',1)
    insert tb  values (3,'abc3',1)
    insert tb  values(4,'abc4',3)
    insert tb  values(5,'abc5',3)
    insert tb  values(6,'abc6',3)
    insert tb  values(7,'abc7',5)declare @lev int
    set @lev = 0
    create table #t(id int ,sname nvarchar(10),parentid int,lev int)
    insert #t select *,@lev from tb where id = 3
    select * from #t
    while @@rowcount > 0
    begin
    set @lev = @lev + 1
    insert #t
    select a.*,@lev from tb a join #t b on a.parentid = b.id and b.lev = @lev - 1 
    end
    select * from #tid          sname      parentid    lev
    ----------- ---------- ----------- -----------
    3           abc3       1           0
    4           abc4       3           1
    5           abc5       3           1
    6           abc6       3           1
    7           abc7       5           2(5 row(s) affected)