--Create table for the storage of id - parent relation
CREATE TABLE pranay(
AutoID INT,
ParentID INT,
Category VARCHAR(20)
)
-- insert data into the table as required
INSERT INTO pranay (AutoID,ParentID,Category) SELECT 1, 0, 'Entertainment'
INSERT INTO pranay (AutoID,ParentID,Category) SELECT 2, 1, 'Hindi Songs'
INSERT INTO pranay (AutoID,ParentID,Category) SELECT 3, 2, 'Romantic'
INSERT INTO pranay (AutoID,ParentID,Category) SELECT 4, 0, 'Technology'
INSERT INTO pranay (AutoID,ParentID,Category) SELECT 5, 4, 'C#'
--Create a stored proc for execution with an input parameter
CREATE Procedure DBO.PranayCategory
@ID INT
AS
BEGIN
;WITH Video AS
(
SELECT *
FROM pranay
WHERE AutoID = @ID
UNION ALL
SELECT t.*
FROM pranay t INNER JOIN
video r ON t.ParentID = r.AutoID
)
SELECT *
FROM video
END
--Execute the procedure as below... change the input parameter accordingly.
exec PranayCategory 4
Hope it helps...
SheoNarayan, if this helps please login to Mark As Answer. | Alert Moderator