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

可以这样玩:

<!-- 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. PTA(Advanced Level)1037.Magic Coupon

    The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, m ...

  2. Oracle学习记录(一)

    一.Oracle历史 于1977年拉里埃里森和他三个朋友成立了软件研发公司,并在之后给中央情报局开发了命名为Oracle而出名,并在之后将公司名改为Oracle.1989年以甲骨文命名进入中国. Or ...

  3. nginx核心模块常用指令

    默认启动Nginx时,使用的配置文件是: 安装路径/conf/nginx.conf 文件,可以在启动nginx的时候,通过-c来指定要读取的配置文件 常见的配置文件有如下几个: nginx.conf: ...

  4. sentinel与hystrix对比

    近期有同事再提要不要使用sentinel.所以我就对现在已经用hystrix.先看两者的线程模型.大部分对比项是sentinel开源工程对比的,本人做了一些修改以及增加了一些对比项和说明. 从线程模型 ...

  5. php.ini中allow_url_fopen和allow_url_include的设置

    all_url_include在php 5.2以后添加,安全方便的设置(php的默认设置)为:allow_url_fopen=on;all_url_include=off;allow_url_fope ...

  6. 使ffmpeg支持HDR10bit 环境为ubuntu16.04

    1. 编译X265,生成静态库, 安装到默认目录 修改CMakeLists.txt 使   HIGH_BIT_DEPTH  设置为ON cmake -G "Unix Makefiles&qu ...

  7. mycat sql timeout 问题解决

    发现程序中有个批量update语句需要update 16000多条数据导致超时 2019-11-06 10:35:28.312 pool-9-thread-24 ERROR com.hp.nova.c ...

  8. Spring @Scheduled执行原理解析

    项目使用很多@Scheduled(cron=**) 注解来实现定时任务,既然要用就必须弄清楚的它的实现原理,于是乎翻了一下相关的源码. Spring 3.0之后增加了调度器功能,提供的@Schedul ...

  9. 基于SAML2.0的SAP云产品Identity Authentication过程介绍

    SAP官网的架构图 https://cloudplatform.sap.com/scenarios/usecases/authentication.html 上图介绍了用户访问SAP云平台时经历的Au ...

  10. RobHess的SIFT代码解析步骤三

    平台:win10 x64 +VS 2015专业版 +opencv-2.4.11 + gtk_-bundle_2.24.10_win32 主要参考:1.代码:RobHess的SIFT源码 2.书:王永明 ...