设为首页 收藏本站
查看: 527|回复: 0

[经验分享] Sql server 获得某一部门下的所有子部门。根据子部门获得它的上级部门。

[复制链接]

尚未签到

发表于 2015-7-2 11:25:49 | 显示全部楼层 |阅读模式
Sql server 获得某一部门下的所有子部门。根据部门获得它的上级部门。以为要用递归呢,原来不需要的,通过自定义函数里,使用个临时表就可以了。@@RowCount作用可真不小啊。

一、准备数据


DSC0000.gif --用户表
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:         adandelion
-- Create date: 2007-12-03
-- 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:         adandelion
-- Create date: 2007-12-03
-- 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)

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-82490-1-1.html 上篇帖子: SQL Server Everywhere 改名字了 下篇帖子: SQL Server 2008 R2主数据服务安装
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表