码迷,mamicode.com
首页 > 数据库 > 详细

sqlserver 书查询 之二

时间:2018-05-19 13:03:15      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:order by   nio   sele   replica   rom   base   SQ   select   join   

WITH tree
AS
(
SELECT ParentId, Organization_ID,1 AS x2level,Organization_Name,
CAST(Organization_Name AS NVARCHAR(max)) x2name,
CAST(+Organization_ID AS NVARCHAR(max)) x2id
FROM base_organization
WHERE ParentId=‘0‘
UNION ALL
SELECT c.ParentId, c.Organization_ID, tree.x2level + 1,c.Organization_Name,
CAST(REPLICATE(‘-‘, x2level * 4) + c.Organization_Name AS NVARCHAR(max)) x2name,
tree.x2id +‘:|:‘+ CAST(c.Organization_ID AS NVARCHAR(max)) x2id
FROM dbo.base_organization c
INNER JOIN tree
ON c.ParentId = tree.Organization_ID
)
SELECT x2name, Organization_ID, ParentId,x2level FROM tree
ORDER BY x2id;

sqlserver 书查询 之二

标签:order by   nio   sele   replica   rom   base   SQ   select   join   

原文地址:https://www.cnblogs.com/mingjing/p/9059966.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!