Sql server 获得某一部门下的所有子部门。根据子部门获得它的上级部门。
2007-12-03 18:17
525 查看
Sql server 获得某一部门下的所有子部门。根据部门获得它的上级部门。以为要用递归呢,原来不需要的,通过自定义函数里,使用个临时表就可以了。@@RowCount作用可真不小啊。
一、准备数据
--用户表
if object_id ('Users','u') is not null
drop table Users
create table Users
(
User_Id int identity(1,1) not null PRIMARY KEY,
User_Name varchar(20)
)
set IDENTITY_INSERT Users on
insert Users(User_Id,User_Name)
select 1,'User1' union all
select 2,'User2' union all
select 3,'User3' union all
select 4,'User4' union all
select 5,'User5' union all
select 6,'User6' union all
select 7,'User7' union all
select 8,'User8' union all
select 9,'User9' union all
select 10,'User10' union all
select 11,'User11'
set identity_insert Users off
--部门表
if object_id ('Dept','u') is not null
drop table Dept
create table Dept
(
Dept_Id int identity(1,1) not null PRIMARY KEY,
Dept_Name varchar(20),
ParentDept_Id int
)
set IDENTITY_INSERT Dept on
insert Dept(Dept_Id,Dept_Name,ParentDept_id)
select 1,'Dept1',0 union all
select 2,'Dept2',0union all
select 3,'Dept3',0 union all
select 4,'Dept1_1',1 union all
select 5,'Dept1_2',1 union all
select 6,'Dept3_1',3 union all
select 7,'Dept3_1_1',6 union all
select 8,'Dept3_1_2',6 union all
select 9,'Dept3_1_3',6
set identity_insert Dept off
--用户部门表
if object_id ('UserDept','u') is not null
drop table UserDept
create table UserDept
(
UserDept_Id int identity(1,1) not null PRIMARY KEY,
Dept_Id int,
User_Id int
)
set IDENTITY_INSERT UserDept on
insert UserDept(UserDept_Id,Dept_Id,User_Id)
select 1,2,1 union all
select 2,2,2union all
select 3,1,5 union all
select 4,3,3 union all
select 5,3,4 union all
select 6,9,11 union all
select 7,9,10 union all
select 8,9,8 union all
select 9,4,6 union all
select 10,7,7 union all
select 11,4,9
set identity_insert UserDept off
二、根据部门IP获得它下面的所有部门
if object_id('UF_GetChildDept','tf') is not null
drop function UF_GetChildDept
go
-- =============================================
-- Author: <Author,,Name> adandelion
-- Create date: <Create Date,,>2007-12-03
-- Description: <Description,,> 根据传入的部门ID,返回它下面的所有子部门。
-- =============================================
create function UF_GetChildDept( @DeptId int )
returns @tb table (id int)
as
begin
insert into @tb
select dept_id from dept where parentdept_id = @deptid
while @@Rowcount >0 --只要有下级节点就循环
begin
insert into @tb
select dept_id --取出刚刚插入的deptid,去部门表里找parentdept_id = deptid的记录。
from dept as a inner join @tb as b on a.parentdept_id = b.id and a.dept_id not in(select id from @tb)
end
return
end
go
select *
from dbo.UF_GetChildDept(7)
三、根据部门IP获得它的上级部门
if object_id('UF_GetParentDept','tf') is not null
drop function UF_GetParentDept
go
-- =============================================
-- Author: <Author,,Name> adandelion
-- Create date: <Create Date,,>2007-12-03
-- Description: <Description,,> 根据传入的部门ID,返回它的上级部门。
-- =============================================
create function UF_GetParentDept( @DeptId int )
returns @tb table (id int)
as
begin
insert into @tb
select parentdept_id from dept where dept_id = @deptid
while @@Rowcount >0 --
begin
insert into @tb
select parentdept_id --
from dept as a inner join @tb as b on a.dept_id = b.id and a.parentdept_id not in(select id from @tb)
end
return
end
go
select *
from dbo.UF_GetParentDept(7)
一、准备数据
--用户表
if object_id ('Users','u') is not null
drop table Users
create table Users
(
User_Id int identity(1,1) not null PRIMARY KEY,
User_Name varchar(20)
)
set IDENTITY_INSERT Users on
insert Users(User_Id,User_Name)
select 1,'User1' union all
select 2,'User2' union all
select 3,'User3' union all
select 4,'User4' union all
select 5,'User5' union all
select 6,'User6' union all
select 7,'User7' union all
select 8,'User8' union all
select 9,'User9' union all
select 10,'User10' union all
select 11,'User11'
set identity_insert Users off
--部门表
if object_id ('Dept','u') is not null
drop table Dept
create table Dept
(
Dept_Id int identity(1,1) not null PRIMARY KEY,
Dept_Name varchar(20),
ParentDept_Id int
)
set IDENTITY_INSERT Dept on
insert Dept(Dept_Id,Dept_Name,ParentDept_id)
select 1,'Dept1',0 union all
select 2,'Dept2',0union all
select 3,'Dept3',0 union all
select 4,'Dept1_1',1 union all
select 5,'Dept1_2',1 union all
select 6,'Dept3_1',3 union all
select 7,'Dept3_1_1',6 union all
select 8,'Dept3_1_2',6 union all
select 9,'Dept3_1_3',6
set identity_insert Dept off
--用户部门表
if object_id ('UserDept','u') is not null
drop table UserDept
create table UserDept
(
UserDept_Id int identity(1,1) not null PRIMARY KEY,
Dept_Id int,
User_Id int
)
set IDENTITY_INSERT UserDept on
insert UserDept(UserDept_Id,Dept_Id,User_Id)
select 1,2,1 union all
select 2,2,2union all
select 3,1,5 union all
select 4,3,3 union all
select 5,3,4 union all
select 6,9,11 union all
select 7,9,10 union all
select 8,9,8 union all
select 9,4,6 union all
select 10,7,7 union all
select 11,4,9
set identity_insert UserDept off
二、根据部门IP获得它下面的所有部门
if object_id('UF_GetChildDept','tf') is not null
drop function UF_GetChildDept
go
-- =============================================
-- Author: <Author,,Name> adandelion
-- Create date: <Create Date,,>2007-12-03
-- Description: <Description,,> 根据传入的部门ID,返回它下面的所有子部门。
-- =============================================
create function UF_GetChildDept( @DeptId int )
returns @tb table (id int)
as
begin
insert into @tb
select dept_id from dept where parentdept_id = @deptid
while @@Rowcount >0 --只要有下级节点就循环
begin
insert into @tb
select dept_id --取出刚刚插入的deptid,去部门表里找parentdept_id = deptid的记录。
from dept as a inner join @tb as b on a.parentdept_id = b.id and a.dept_id not in(select id from @tb)
end
return
end
go
select *
from dbo.UF_GetChildDept(7)
三、根据部门IP获得它的上级部门
if object_id('UF_GetParentDept','tf') is not null
drop function UF_GetParentDept
go
-- =============================================
-- Author: <Author,,Name> adandelion
-- Create date: <Create Date,,>2007-12-03
-- Description: <Description,,> 根据传入的部门ID,返回它的上级部门。
-- =============================================
create function UF_GetParentDept( @DeptId int )
returns @tb table (id int)
as
begin
insert into @tb
select parentdept_id from dept where dept_id = @deptid
while @@Rowcount >0 --
begin
insert into @tb
select parentdept_id --
from dept as a inner join @tb as b on a.dept_id = b.id and a.parentdept_id not in(select id from @tb)
end
return
end
go
select *
from dbo.UF_GetParentDept(7)
相关文章推荐
- 获得某一部门下的所有子部门
- SQL Server利用递归把所有【子部门员工】汇总到【一级根节点部门】
- 根据某个部门ID递归获取其下面所有子部门和本部门信息
- Sql Server根据表名获得所有列及其属性
- 根据年份-月份,获得此月份的所有日期
- 在Oracle中,快速获取一个部门以及它下面所有的子部门的方法。
- SQL Server 根据表名获取表的所有列及属性(数据同步服务数据分析需要)
- sql server获取某一类别及该类别下的所有子类别
- 根据实例化bean用get,set获得所有属性值
- 根据文件路径获得其所在文件夹,并删除该文件夹下后缀名为.tmp的所有文件
- SQL server 2005 表中某一字段的所有值逗号分开的列表
- SQL SERVER 2000 遍历父子关系数据的表(二叉树)获得所有子节点 所有父节点及节点层数函数
- 如何根据进程名称获得所有进程ID
- SQL Server Update 所有表的某一列(列名相同,类型相同)数值
- 列出薪金高于公司平均薪金所有员工,所在部门,上级领导,公司的工资等级
- 取某个部门下的所有子部门SQL语句
- 数组中查找所有的上级 并组成新的数组(根据条件可以再修改)
- SQL Server公用表达式CET递归查询所有上级数据
- SQL获取SQL Server 根据表名获取表的所有列及属性
- 无限级根据Id获得所有子节点数据