鉴于此表...
CREATE TABLE [dbo].[Data](
[Id] [int] NOT NULL,
[Name] [varchar](50) NOT NULL,
[ParentId] [int] NULL,
CONSTRAINT [PK_Data] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
和相关数据...
BEGIN TRANSACTION;
INSERT INTO [dbo].[Data]([Id], [Name], [ParentId])
SELECT 1, N'George', 4 UNION ALL
SELECT 2, N'Amit', 1 UNION ALL
SELECT 3, N'Chad', 1 UNION ALL
SELECT 4, N'David', NULL UNION ALL
SELECT 5, N'Tess', 3 UNION ALL
SELECT 6, N'Emma', 3
COMMIT;
RAISERROR (N'[dbo].[Data]: Insert Batch: 1.....Done!', 10, 1) WITH NOWAIT;
GO
您能否为我提供 2 个单独的 CTE SQL 来返回给定 ID 的后代和祖先?