树形结构部门的 sqlserver 排序

因为要实现部门排序功能,而且要考虑部门的层级,直接用 sql 排序是不行的,所以写个 sql function 来支持。 
首先部门表:company

CREATE TABLE company(
CompanyId id NOT NULL,
CompanyName nvarchar(115) NOT NULL

记录部门层级结构的表,如果部门没有上级部门则在这张表中不会有记录

CREATE TABLE company_report(
CompanyId id NOT NULL,
ReportToId id NOT NULL,
DisplayOrd ord CONSTRAINT [DF1_company_report] DEFAULT (1) NOT NULL
)

在 company_report 中 ReportToId 是指上级部门的 CompanyId 。 
像这种树形结构,在代码中一般都是用递归来遍历了,但是在 sql 中实现递归还是很麻烦的,还是写成循环简单点。 
定义 function :

go
if (exists (select * from sys.objects where name = 'get_company_report_name_fn'))
drop FUNCTION get_company_report_name_fn
go
CREATE FUNCTION get_company_report_name_fn (@i_vCompanyId id, @i_vCompanyName string2)
RETURNS string2
AS
BEGIN
DECLARE @t_vResult string2;
DECLARE @t_vReportToId id; SET @t_vResult = '';
--父部门ID
SET @t_vReportToId = 0;
--拼接父部门Name
SELECT @t_vResult = r.CompanyName + '_' + c.CompanyName,
@t_vReportToId = cr.ReportToId
FROM company_report cr, company c, company r
WHERE cr.CompanyId = c.CompanyId
AND cr.ReportToId = r.CompanyId
AND cr.CompanyId = @i_vCompanyId
--while 父部门还存在父部门
while (
exists(select cr.ReportToId from company_report cr where cr.CompanyId = @t_vReportToId)
)
begin
SELECT @t_vResult = r.CompanyName + '_'+ @t_vResult,
@t_vReportToId = cr.ReportToId
FROM company_report cr, company c, company r
WHERE cr.CompanyId = c.CompanyId
AND cr.ReportToId = r.CompanyId
AND cr.CompanyId = @t_vReportToId
end
--已经是最顶层的部门了 返回原值
if @t_vResult = ''
begin
SET @t_vResult = @i_vCompanyName
end return @t_vResult
END
GO

原理就是在子部门的 name 上加上父部门的 name 用 _ 符号连接,如果父部门还存在父部门则继续连接下去。 
在排序的时候这样调用:

select dbo.get_company_report_name_fn(companyId, companyName) from company order by dbo.get_company_report_name_fn(companyId, companyName)

结果:

ula-client01 LTD.
ula-client01 LTD._ula-client02
ula-client01 LTD._ula-client02_ula-client02-子
ula-client01 LTD._ula-client03
Sony
Sony_Hair
Sony_Hair_IBM

写完给 Leader 看看,他觉得我写复杂了,然后就随手改了下:

go
if (exists (select * from sys.objects where name = 'get_company_report_name_fn'))
drop FUNCTION get_company_report_name_fn
go
CREATE FUNCTION get_company_report_name_fn (@i_vCompanyId id, @i_vCompanyName string2)
RETURNS string2
AS
BEGIN
DECLARE @t_vResult string2;
DECLARE @t_vReportToId id;
DECLARE @t_vReportToName string2; SET @t_vResult = @i_vCompanyName;
SET @t_vReportToId = @i_vCompanyId; while (exists(select cr.ReportToId from company_report cr where cr.CompanyId = @t_vReportToId))
begin
SELECT @t_vReportToId = cr.ReportToId, @t_vReportToName = c.companyName
FROM company_report cr, company c
WHERE cr.ReportToId = c.CompanyId
AND cr.CompanyId = @t_vReportToId;
set @t_vResult = @t_vReportToName + '_' + @t_vResult;
end return @t_vResult;
END
go

好吧,是简单了很多。主要是消除了重复的代码。 
END。

树形结构部门的 sqlserver 排序的更多相关文章

  1. YbSoftwareFactory 代码生成插件【十八】:树形结构下的查询排序的数据库设计

    树形结构的排序在中国特色下十分普遍也非常重要,例如常说的五大班子,党委>人大>政府>政协>纪委,每个班子下还有部门,岗位,人员,最终排列的顺序通常需要按权力大小.重要性等进行排 ...

  2. 部门树形结构,使用Treeview控件显示部门

    部门树形结构.设计张部门表用于存储部门编码.名称.上级部门id,使用Treeview控件显示部门树,并实现部门增删改.移动.折叠等功能.特别提示,部门有层级关系,可用donetbar的adtree控件 ...

  3. js文章列表的树形结构输出

    文章表设计成这样了 后端直接给了无任何处理的json数据,现在要前端实现树形结构的输出,其实后端处理更简单写,不过既然来了就码出来 var doclist = [{ "id": 1 ...

  4. C# EasyUI树形结构权限管理模块

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 本节和大家探讨下C#使用EasyUI树形结构/Tree构 ...

  5. Java创建树形结构算法实例

    在JavaWeb的相关开发中经常会涉及到多级菜单的展示,为了方便菜单的管理需要使用数据库进行支持,本例采用相关算法讲数据库中的条形记录进行相关组装和排序讲菜单组装成树形结构. 首先是需要的JavaBe ...

  6. Atitit 常见的树形结构 红黑树  二叉树   B树 B+树  Trie树 attilax理解与总结

    Atitit 常见的树形结构 红黑树  二叉树   B树 B+树  Trie树 attilax理解与总结 1.1. 树形结构-- 一对多的关系1 1.2. 树的相关术语: 1 1.3. 常见的树形结构 ...

  7. MongoDB五种树形结构表示法

    MongoDB五种树形结构表示法 第一种:父链接结构 db.categories.insert( { _id: "MongoDB", parent: "Databases ...

  8. Android - N级树形结构实现

    目前已经实现3级之内的任意级树形结构展示(如果想增加更多级,需要扩展排序算法),并支持单选和多选(使用不同的适配器). 实现使用的控件:ListView 首先,最重要的应该是数据源的格式,支持树形结构 ...

  9. 关于mysql中数据存储复合树形结构,查询时结果按树形结构输出

    1.主要思想:根据已有数据,规则性的造数据 select * FROM(select lId,strName,lId as lParentId,-1 as orderIdx from tbClassi ...

随机推荐

  1. Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp

    题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...

  2. bzoj 1196 二分+生成树判定

    我们先二分一个答案,对于每个答案,先加一级公路,如果不够k直接break, 然后再加二级公路,加的过程类似Kruskal. /************************************* ...

  3. 一个包的TcpServer流程

    上次说到对于那种有内容的包 bool TCPServer::on_receive_data(int channel_id, void* data, int len) { packet pkt; { p ...

  4. WebService流行框架之Axis和CXF

    转自:http://www.cnblogs.com/snake-hand/archive/2013/06/09/3129915.html 前言 上节课我们对WebService进行了简单的介绍,对于其 ...

  5. 在centos 6.5 在virtual box 上 安装增强版工具

    centos 6.5 在virtual box 上 安装增强版工具: 出现:centos unable to find the source of your current linux kernel ...

  6. 微信电脑版也能用公众号自定义菜单 微信1.2 for Windows发布

    昨日,微信电脑版发布更新,版本为微信1.2 for Windows,最大的特色就是加入了保存聊天记录功能,可以使用公账号菜单,手机上收藏的表情也能在电脑版上发送,可以接收转账消息. 本次微信pc版更新 ...

  7. Chapter 4

    1.Python中有四种函数:全局函数,局部函数,lambda函数,方法.其中全局函数与局部函数对比理解,局部函数就是定义在某函数之内的函数,否则就是全局函数,lambda函数就是表达式,方法就跟对象 ...

  8. linux下通过acl配置灵活目录文件权限(可用于ftp,web服务器的用户权限控制)

    linux下通过acl配置灵活目录文件权限(可用于ftp,web服务器的用户权限控制) 发表于2012//07由feng linux 本身的ugo rwx的权限,对于精确的权限控制很是力不从心的,ac ...

  9. LA 4727

    Integers 1, 2, 3,..., n are placed on a circle in the increasing order as in the following figure. W ...

  10. uva 11090

    I I U P C 2 0 0 6 Problem G: Going in Cycle!! Input: standard input Output: standard output You are ...