日期:2014-05-18  浏览次数:20431 次

问一条SQl语句实现
如何用SQl语句实现查询一个部门下的所有员工?
  表结构如下: 部门表 Department 字段( ID ,DepartmentName , ParentDepartment)
  员工表 Employee 字段( ID ,EmployeeName , ParentID )
  因为部门下面又有子部门,所以要查出这个部门下及其子部门下的所有员工,..
只用SQL实现如何实现?

------解决方案--------------------
SQL code
--生成测试数据
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

------解决方案--------------------
部门表 Department 字段( ID ,DepartmentName , ParentDepartment) 
员工表 Employee 字段( ID ,EmployeeName , ParentID ) 
SQL code


create function wsp(@ID VARCHAR(10))
returns varchar(500)
as
begin
    declare @i int
    declare @t table(ID int,ParentDepartment int,Level INT)
    set @i = 1
    insert into @t select ID,ParentDepartment,@i from Department where ID = @ID
    while @@rowcount<>0
    begin
        set @i = @i + 1
        insert into @t select a.ID,a.ParentDepartment,@i 
        from Department a,@t b where a.ParentDepartment=b.ID and b.Level = @i-1
    end
    declare @sql varchar(500)
    select @sql=isnull(@sql+',','')+ltrim(id) from @t 
    return @sql
end
go

--调用:

select * from Employee where charindex(','+ltrim(ParentID)+',',','+dbo.wsp(部门id)+',')>0

------解决方案--------------------
SQL code
--测试数据
CREATE TABLE tb(ID char(3),PID char(3),Name nvarchar(10))
INSERT tb 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','小分市'
GO

--查询指定节点及其所有子节点的函数
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  招远市
--*/

------解决方案--------------------
SQL code
create table tb(id int, name varchar(10), pid int, px int)
insert into tb values(0 , '栏目分类', 0 , 1)
insert into tb values(1 , '动物' , 0 , 1) 
insert into tb values(2 , '视频' , 0 , 2) 
insert into tb values(3 , '老虎' , 1 , 1) 
insert into tb values(4 , '狮子' , 1 , 2) 
insert into tb values(5 , '搞笑' , 2 , 1) 
go

--查询指定节点及其所有子节点的函数 
CREATE FUNCTION f_Cid(@ID int) RETURNS @t_Level TABLE(ID int,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 

--调用函数查询id = 1及其所有子节点 
SELECT a.* FROM tb a,f_Cid(1)