可能重复:
SQL:按特定顺序从自引用表中删除数据
我需要从 SQL Server 2008 中的自引用表中删除一组记录。我正在尝试执行以下操作,但它不像 order by。
WITH SelfReferencingTable (ID, depth)
AS
(
SELECT id, 0 as [depth]
FROM dbo.Table
WHERE parentItemID IS NULL AND [t].ColumnA = '123'
UNION ALL
SELECT [t].ID, [srt].[depth] + 1
FROM dbo.Table t
INNER JOIN SelfReferencingTable srt ON [t].parentItemID = [srt].id
WHERE [t].ColumnA = '123'
)
DELETE y FROM dbo.Table y
JOIN SelfReferencingTable x on x.ID = y.id
ORDER BY x.depth DESC
任何想法为什么这不起作用?