-- SQL2005 递归查询示例,非常方便
-- Author:BOBBY
WITH tree AS
(SELECT F_DEPT_ID, CAST(F_DEPT_NAME AS varchar(MAX)) AS F_DEPT_NAME
FROM dbo.T_ACCOUNTS_DEPT_INFO AS A
WHERE (F_PARENT_DEPTID = 0)
UNION ALL
SELECT A.F_DEPT_ID, B.F_DEPT_NAME + '/' + A.F_DEPT_NAME AS F_DEPT_NAME
FROM dbo.T_ACCOUNTS_DEPT_INFO AS A CROSS JOIN
tree AS B
WHERE (A.F_PARENT_DEPTID = B.F_DEPT_ID))
SELECT TOP (100) PERCENT F_DEPT_ID, F_DEPT_NAME
FROM tree AS tree_1
ORDER BY F_DEPT_NAME
-- Author:BOBBY
WITH tree AS
(SELECT F_DEPT_ID, CAST(F_DEPT_NAME AS varchar(MAX)) AS F_DEPT_NAME
FROM dbo.T_ACCOUNTS_DEPT_INFO AS A
WHERE (F_PARENT_DEPTID = 0)
UNION ALL
SELECT A.F_DEPT_ID, B.F_DEPT_NAME + '/' + A.F_DEPT_NAME AS F_DEPT_NAME
FROM dbo.T_ACCOUNTS_DEPT_INFO AS A CROSS JOIN
tree AS B
WHERE (A.F_PARENT_DEPTID = B.F_DEPT_ID))
SELECT TOP (100) PERCENT F_DEPT_ID, F_DEPT_NAME
FROM tree AS tree_1
ORDER BY F_DEPT_NAME