sqlserver 书查询 之二

Posted 明净

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sqlserver 书查询 之二相关的知识,希望对你有一定的参考价值。

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 书查询 之二的主要内容,如果未能解决你的问题,请参考以下文章

SQLServer 2008中SQL增强之二 Top新用途

MSSQL之二 Sql Server中管理库与表

Sqlserver 2005 多表查询

sqlserver查询很慢、而且超时为啥?

sqlserver多表查询

SqlServer系列子查询