SQL Server 2008 Delete Records from Self-Referencing Table in correct order

Posted by KTrace on Stack Overflow See other posts from Stack Overflow or by KTrace
Published on 2012-08-31T15:30:29Z Indexed on 2012/09/01 9:38 UTC
Read the original article Hit count: 183

I need to delete a sub set of records from a self-referencing table in SQL Server 2008. I am trying to do the following but it is does not like the 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

Any ideas why this isn't working?

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server-2008