摘要:利用CTE递归查询(WITH common_table_expression (Transact-SQL))
CTE的官方解释是:
示例代码:
;with tmp_num as
(
select convert(int,1) as num,convert(varchar(max),'┌') as path_name
union all select convert(int,num+1) as num,convert(varchar(max),'├' + REPLICATE('-',num+1))
as path_name from tmp_num where num<32767
)
select path_name + convert(varchar(10),num) as title,* from tmp_num OPTION(MAXRECURSION 32767)
运行结果如下:
详细的解释,请参阅官方资料:
http://technet.microsoft.com/zh-cn/library/ms175972.aspx