【发布时间】:2012-08-31 15:30:29
【问题描述】:
可能重复:
SQL: DELETE data from self-referencing table in specific order
我需要从 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
任何想法为什么这不起作用?
【问题讨论】:
标签: sql sql-server-2008 sql-order-by sql-delete self-reference