一棵树的层次结构都在一张表内,当有这样的需要的时候。。

可以这样玩:

<!-- DepartmentDTO 对象对应 department表_查询sql -->
<sql id="department_select_sql">
with ldepartment as (
select
dept_id,
parent_id,
0 as dept_level,
row_number()over(order by getdate()) as orderid
from
department
where
(
parent_id is null
or parent_id = ''
)
union all
select
a.dept_id,
a.parent_id,
b.dept_level + 1 as dept_level,
b.orderid*100+row_number()over(order by getdate()) as orderid
from
department a,
ldepartment b
where
a.parent_id = b.dept_id
) select
t1.dept_level,
t1.orderid,
t2.paic_unique_deptid,
t2.deptid_descr,
t2.parent_id,
(select aa.deptid_descr + '('+aa.dept_id+')' from department aa where aa.dept_id=t2.parent_id ) as parent_id_desc,
t2.dept_id,
t2.ou_type,
t2.date_created,
t2.created_by,
t2.date_updated,
t2.updated_by,
t2.row_id
from ldepartment t1, department t2
where t1.dept_id = t2.dept_id
<isNotEmpty prepend="and" property="deptid_descr">
t2.deptid_descr like '%+#deptid_descr#+%'
</isNotEmpty>
<isNotEmpty prepend="and" property="parent_id">
t2.parent_id = #parent_id#
</isNotEmpty> order by ltrim(t1.orderid) // 关键点(字符串排序)
</sql>

调用:

<select id="department_find" parameterClass="java.util.Map"
resultClass="com.pasc.supms.parameter.dto.DepartmentDTO">
<include refid="department_select_sql" />
</select>

java对象:

public class DepartmentDTO extends SupmsBaseDTO {

    private String paic_unique_deptid; // 部门唯一编号
private String deptid_descr; // 部门名称
private String parent_id; // 上级部门编号
private String parent_id_desc; // 上级部门名称编号
private String dept_id; // 部门属主编号
private String ou_type; // 部门类型
private String dept_level; // 部门层级

jsp页面:

<table cellpadding="0" cellspacing="0" class="table_list_2" >
<thead align="center">
<tr>
<th >部门编号</th>
<th >部门名称</th>
<th >上级部门编号</th>
<th >部门类型</th>
</tr>
</thead>
<tbody align="center">
<c:choose>
<c:when test="${not empty pageBean.resultList}">
<c:forEach var="doc" items="${pageBean.resultList}" varStatus="i">
<tr>
<td><!-- <a href="#" onclick="detailDo('${doc.row_id}');" title="点击查看详情" class="blue"></a> -->
<c:forEach begin="1" end="${doc.dept_level }">--&nbsp;</c:forEach>
${doc.dept_id }</td>
<td>${doc.deptid_descr }</td>
<td>${doc.parent_id_desc }</td>
<td>${doc.ou_type }</td>
</tr>
</c:forEach>
</c:when>
<c:otherwise>
<tr id="noList">
<td colspan="11" align="center">对不起,暂时还没有记录!</td>
</tr>
</c:otherwise>
</c:choose>
</tbody>
</table>

结果:

sql server 遍历表成一棵树结构的更多相关文章

  1. SQL Server遍历表的几种方法 转载

    SQL Server遍历表的几种方法 阅读目录 使用游标 使用表变量 使用临时表 在数据库开发过程中,我们经常会碰到要遍历数据表的情形,一提到遍历表,我们第一印象可能就想到使用游标,使用游标虽然直观易 ...

  2. SQL Server遍历表中记录的2种方法

    SQL Server遍历表一般都要用到游标,SQL Server中可以很容易的用游标实现循环,实现SQL Server遍历表中记录.本文将介绍利用使用表变量和游标实现数据库中表的遍历. 表变量来实现表 ...

  3. SQL Server遍历表的几种方法

    在数据库开发过程中,我们经常会碰到要遍历数据表的情形,一提到遍历表,我们第一印象可能就想到使用游标,使用游标虽然直观易懂,但是它不符合面向集合操作的原则,而且性能也比面向集合低.当然,从面向集合操作的 ...

  4. sql server遍历表不用游标和临时表的方法

    表结果如图 )) ,'Sky,Blue,Water' ,'Book,Apple,Shirt' ,'Cup,Yellow,org' ,'box,phone,paper' GO SELECT id,SUB ...

  5. sql while 遍历表

    declare @n int declare @rowcount int ) create table #temp ( id ,), employeeName ) ) insert into #tem ...

  6. SQL Server中如何实现遍历表的记录

    SQL Server遍历表一般都要用到游标,SQL Server中可以很容易的用游标实现循环,实现SQL Server遍历表中记录. 但游标在实际的开发中都不推荐使用. 我们知道还可以借助临时表或表变 ...

  7. SQL Server系列

    这里整理了我学习SQL Server的全部文章,包括从基础到高级,做一个目录,方便以后查找. SQL Server数据类型 SQL Server中开发常用的数据类型 单表查询和多表查询 一个单表查询的 ...

  8. Expression构建DataTable to Entity 映射委托 sqlserver 数据库里面金额类型为什么不建议用float,实例告诉你为什么不能。 sql server 多行数据合并成一列 C# 字符串大写转小写,小写转大写,数字保留,其他除外 从0开始用U盘制作启动盘装Windows10系统(联想R720笔记本)并永久激活方法 纯CSS打造淘宝导航菜单栏 C# Winform

    Expression构建DataTable to Entity 映射委托   1 namespace Echofool.Utility.Common { 2 using System; 3 using ...

  9. SQL SERVER 中 实现主表1行记录,子表多行记录 整合成一条虚拟列

    表中有这样的记录,简单的主子表,现要想通过left join 语句把两表关联起来 select * from tbl_diary_reback a left join tbl_diary_reback ...

随机推荐

  1. Android 变量取名神器

    前言 在工作中,我们还在为起变量名而苦恼吗?今天无意间发现一个专门为变量取名而诞生的神器 codelf. 我们可以直接浏览器访问 http://unbug.github.io/codelf/ 现在我们 ...

  2. 「java.util.concurrent并发包」之 CopyOnWrite

    一 CopyOnWrite容器概述 Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容,当某个人想要修改这个内容的时候,才会真正把内容C ...

  3. Linux系列(16)之系统资源的观察

    1.系统资源观察 1.观察内存使用情况:free 格式: free //默认显示的单位为KBytes,显示系统的内存容量 free  [-b | -k | -m | -g | -h]  [-t]  [ ...

  4. ffmpeg解码音视频过程(附代码)

    0. 引言 最近一直在使用和学习ffmpeg. 工作中需要拉流解码, 获取音频和视频数据. 这些都是使用ffmpeg处理. 因为对ffmpeg接触不多, 用的不深, 在使用的过程中经常遇到不太懂的地方 ...

  5. centos8自定义目录安装nginx

    1.安装工具和库 # PCRE是一个Perl库,包括 perl 兼容的正则表达式库.nginx 的 http 模块使用 pcre 来解析正则表达式 # zlib库提供了很多种压缩和解压缩的方式, ng ...

  6. 用python打开文件夹的三种方式

    一.利用explorer.exe import os # 利用explorer.exe执行 start_directory = r'C:\代码\软件包' os.system("explore ...

  7. tomcat 发布的web项目不在webapps目录下

    双击服务器(如果服务器再启动,请停止并删除里面的项目,再clean一下), server location 选择use tomcat installation: deploy path 改为webap ...

  8. k8s-jenkins pipeline部署

  9. 解决python在cmd运行时导入包失败,出现错误信息 "ModuleNotFoundError: No module named ***"

    1.下图为我的自动化测试工程结构图 我通过运行run.bat批处理文件,调用cmd控制台运行start_run.py来开始我的自动化测试,但是出现如下错误: 大家可能知道我们的工程在IDE(Pycha ...

  10. MediaAPIController

    using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.D ...