欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SQL Server 实现递归查询

程序员文章站 2022-06-22 16:05:50
基础数据/表结构 Sql 语句 结果 ......
  • 基础数据/表结构

SQL  Server 实现递归查询               SQL  Server 实现递归查询

  • Sql 语句
1 ;With cte(id,pid,TName)As  
2     (
3     Select id,pid,TName
4     From  Test   where pid = 1
5     Union All 
6     Select B.id,B.pid,B.TName
7     From cte Inner Join Test B On cte.id = B.pid 
8     )
9  select * from cte  order by id 
  • 结果

SQL  Server 实现递归查询