DECLARE @T Table (ID int, Name varchar(50), ParentId INT)...

0

Please login or register to vote for this query.

(click on this box to dismiss)

Audio-Video Production Meta

Q&A about the site for engineers, producers, editors, and enthusiasts spanning the fields of video, and media creation

DECLARE @T Table (ID int, Name varchar(50), ParentId INT);
DECLARE @itemToDelete INT = 4;
INSERT INTO @t VALUES
(1  ,'Home',                    0),
(2  ,'Products',                0),
(3  ,'SubProduct1',         2),
(4  ,'SubProduct2',         2), 
(5  ,'Subsub',           4),
(6  ,'ii. ......',          4),
(7  ,'About',                   0);

;WITH ParentChildsTree
AS
(
    SELECT ID, Name, ParentId
    FROM @T
    WHERE Id = @itemToDelete
    UNION ALL
    SELECT t.ID, t.Name, t.ParentId
    FROM ParentChildsTree c
    INNER JOIN @T t ON c.ParentId = t.Id
)
DELETE FROM @T 
WHERE ID IN (SELECT ID FROM ParentChildsTree);



sELECT * FROM @T;

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results