SQL笔记 - CTE递归实例(续):显示指定部门的全称

时间:2023-03-10 04:23:57
SQL笔记 - CTE递归实例(续):显示指定部门的全称

前一篇文章中已经可以取得所有部门的全称,但现在又有个新的需求: 只想得到某一个部门的部门全称,虽然可以用where条件来过滤,但是会有点小浪费。 这时我们可以从后往前找,先看下效果:

SQL笔记 - CTE递归实例(续):显示指定部门的全称

最后一条就是,行得通! 但是怎么取出来呢? 用ParentUnitID排序? 但是实际生活中,部门可能调整或归并,并不总是 UnitID > ParentUnitID. 所以需要一个类似于 标识列的参照物:

 Declare @utid int
Set @utid = 10 -- the target unit
;
With CTE_Unit_Name_Special -- test: show the full name of every Unit
as(
select UnitID,
--UnitName,
Cast(UnitName as nvarchar(max)) as UnitName,
ParentUnitID,
0 as IndexTemp -- identity
from Unit
where UnitID = @utid
Union All -- Essential
select U.UnitID,
(
U.UnitName + '/' + CU.UnitName
) as UnitName,
U.ParentUnitID,
(CU.IndexTemp + 1) as IndexTemp
from Unit as U
Inner Join CTE_Unit_Name_Special as CU
on U.UnitID = CU.ParentUnitID
where U.ParentUnitID != 0
)
--select * from CTE_Unit_Name_Special
select top 1 * from CTE_Unit_Name_Special
--order by ParentUnitID asc -- only the situation where PUTID < UTID is suited
order by IndexTemp desc -- need a reference substance, like a Identity column

结果不用再显示了。。。 等等,刚想起来,部门表中有个列UnitLevel是标识部门父子层次关系的,不管部门怎么调整,这个层次还是有顺序的, 可以直接用, 一样的。。。。

顺便捎带个以前写的一个函数:

 -- Function - get a full unit name of one special unit
Create Function FN_GetFullUnitName(@unitID int)
Returns nvarchar(1000)
as
Begin
Declare @fullName nvarchar(1000),
@level smallint,
@parentUTID int
select @fullName = UnitName,
@parentUTID = ParentUnitID,
@level = UnitLevel
from Unit
where UnitID = @unitID if @level <= 2
return @fullName while @level > 2
Begin
Set @unitID = @parentUTID
select @fullName = UnitName + '/' + @fullName,
@parentUTID = ParentUnitID,
@level = UnitLevel
from Unit
where UnitID = @unitID
End
return @fullName
End
go
--Drop Function FN_GetFullUnitName
--select dbo.FN_GetFullUnitName(10) -- 销售部/售后科/客服部