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

Sqlserver 树形查询

时间:2015-02-05 17:47:32      阅读:349      评论:0      收藏:0      [点我收藏+]

标签:

WITH tree

AS
(
SELECT dwpid, dwid,1 AS x2level,dwmc,
/*CAST(dwmc AS NVARCHAR(max)) x2name,*/
CAST(+dwid AS NVARCHAR(max)) x2id
FROM cf_xzdw
WHERE dwpid =‘0‘
UNION ALL
SELECT c.dwpid, c.dwid, tree.x2level + 1,c.dwmc,
/*CAST(REPLICATE(‘-‘, x2level * 4) + c.dwmc AS NVARCHAR(max)) x2name,*/
tree.x2id +‘-‘+ CAST(c.dwid AS NVARCHAR(max)) x2id
FROM cf_xzdw c
INNER JOIN tree
ON c.dwpid = tree.dwid
)
SELECT dwmc, dwid, dwpid FROM tree
ORDER BY x2id;

 

参考:

http://blog.chinaunix.net/uid-451-id-3344447.html

Sqlserver 树形查询

标签:

原文地址:http://www.cnblogs.com/liuzhixian/p/4275119.html

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