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)

转载于:https://www.cnblogs.com/adandelion/archive/2007/12/03/981392.html

Sql server 获得某一部门下的所有子部门。根据子部门获得它的上级部门。相关推荐

  1. MS sql server 基础知识回顾(二)-表连接和子查询

    五.表连接 当数据表中存在许多重复的冗余信息时,就要考虑将这些信息建在另一张新表中,在新表中为原表设置好外键,在进行数据查询的时候,就要使用到连接了,表连接就好像两根线,线的两端分别连接两张表的不同字 ...

  2. SQL Server维护计划–好处,功能和特性

    There is a range of functionalities that SQL Server Maintenance Plans offers, and these are designed ...

  3. 如何在SQL Server中使用级联删除?

    本文翻译自:How do I use cascade delete with SQL Server? I have 2 tables: T1 and T2, they are existing tab ...

  4. azure不支持哪些语句 sql_SQL Azure vs SQL Server

    公告:本博客为微软云计算中文博客的镜像博客.部分文章因为博客兼容性问题,会影响阅读体验.如遇此情况,请访问. 概要SQL Azure Database 是一个来自微软的,基于云的关系型数据库服务.SQ ...

  5. C#毕业设计——基于C#+asp.net+SQL server的办公自动化管理系统设计与实现(毕业论文+程序源码)——办公自动化管理系统

    基于C#+asp.net+SQL server的办公自动化管理系统设计与实现(毕业论文+程序源码) 大家好,今天给大家介绍基于C#+asp.net+SQL server的办公自动化管理系统设计与实现, ...

  6. php charindex,SQL Server Charindex()函数

    在本教程中,将学习如何使用SQL Server CHARINDEX()函数来搜索字符串中的子字符串. SQL Server CHARINDEX()函数简介 SQL Server CHARINDEX() ...

  7. SQL Server 2008高可用性系列:数据库快照

    SQL Server 2008高可用性系列:数据库快照 http://database.51cto.com  2010-09-13 14:45  我爱菊花  博客园  我要评论(0) 摘要:我们今天要 ...

  8. T-SQL Enhancement in SQL Server 2005[上篇]

    较之前一版本,SQL Server 2005可以说是作出了根本性的革新.对于一般的编程人员来说,最具吸引力的一大特性就是实现了对CLR的寄宿,使我们可以使用任意一种.NET Programming L ...

  9. 十步优化SQL Server中的数据访问

    故事开篇:你和你的团队经过不懈努力,终于使网站成功上线,刚开始时,注册用户较少,网站性能表现不错,但随着注册用户的增多,访问速度开始变慢,一些用户开始发来邮件表示抗议,事情变得越来越糟,为了留住用户, ...

最新文章

  1. ijkplayer支持h264
  2. [openmp]使用嵌套互斥锁锁定变量
  3. 来认识下less css
  4. 两片关于NAND FLASH的好博客
  5. Extjs4.2或以上 使用自定义事件时报错问题
  6. 【优化算法】龙格-库塔优化算法【含Matlab源码 1799期】
  7. vue-cli创建项目实例
  8. 《啊哈算法》知识点总结
  9. putty连接linux设置文件夹,【整理】Windows用ssh连接Linux,想要从Linux上面上传/下载文件 - putty的子工具psftp...
  10. java环境配置(jdk、jre安装和环境配置)
  11. 单片机c语言全解 pdf,单片机c语言程序.pdf
  12. 计算机上的符号在哪找,像w的那个符号在哪里可以找到?word文档
  13. js控制从绿色到红色的渐变
  14. Unity获取组件的几种方式(拖拽法、标签法、名字法)
  15. AMD提出的补丁使退出延迟降低21%左右
  16. 万字长文让您搞懂云原生!
  17. 常用的excel公式备忘
  18. SRAM/SDRAM/DDR/Cache
  19. Reverse Vowels
  20. [hdu6595]Everything Is Generated In Equal Probability

热门文章

  1. python官方推荐的三本书-【数据分析】入门数据分析,你一定要看的三本书
  2. python读取txt数据-Python从文件中读取数据
  3. python话雷达图-PYTHON绘制雷达图代码实例
  4. python工资等级分类程序-php项目中用python来预测薪资(工资)
  5. python3爬虫系列教程-Python3爬虫视频学习教程
  6. python批量下载网页文件-Python实现批量下载文件
  7. python编程需要什么软件-《》 学习python编程需要安装哪些软件?
  8. 学python需要学数据库吗-学习python用什么数据库好?
  9. NVIDIA Jetson NX开发板在U盘挂载的时候出现:error mounting unknown filesystem type ‘exfat‘错误
  10. ML-1 逻辑回归和梯度下降