行万里路

行万里路,也要读万卷书

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: :: 管理 ::

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

posted on 2015-02-05 16:11  贤言叙语  阅读(970)  评论(0编辑  收藏  举报