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

SqlServer递归查询

程序员文章站 2023-11-13 19:04:22
1 DECLARE @ParentId NVARCHAR(36); 2 SET @ParentId = '078499bf-dedd-b293-4653-7bf6a2e54cbb'; 3 4 WITH [Temp] AS 5 ( 6 SELECT [A].* FROM [dbo].[Contacts ......
 1 DECLARE @ParentId NVARCHAR(36);
 2 SET @ParentId = '078499bf-dedd-b293-4653-7bf6a2e54cbb';
 3 
 4 WITH [Temp] AS
 5 (
 6     SELECT [A].* FROM [dbo].[Contacts] A WHERE [A].[ParentID] = @ParentId AND [A].[IsDefault] = 1
 7     UNION ALL
 8     SELECT [A].* FROM [dbo].[Contacts] A INNER JOIN [Temp] B ON A.[ParentID] = B.[ObjectID] AND [A].[IsDefault] = 1
 9 )
10 SELECT * FROM [Temp] ORDER BY [Temp].[GlobalSort] ASC;