create table dbo.text_master_test
(
text_id int,
text_details nvarchar(max),
new_text_id int
)
go
insert into text_master_test
values(1, 'det 1',2), (2, 'det 2',3), (3, 'det 3',4), (4, 'det 4',5), (5, 'det 5',5);
go
WITH textHierarchy AS (
SELECT tm.text_id, tm.new_text_id, nullif(tm.new_text_id, tm.text_id) as next_text_id
FROM text_master_test tm
WHERE tm.text_id = 1
UNION ALL
SELECT tm.text_id, tm.new_text_id, nullif(tm.new_text_id, tm.text_id) as next_text_id
FROM text_master_test as tm
JOIN textHierarchy AS txtHr ON tm.text_id = txtHr.next_text_id
)
SELECT * FROM textHierarchy;
go
create function dbo.textrecursion(@start_text_id int)
returns table
as
return
(
WITH textHierarchy
AS
(
SELECT tm.text_id, tm.text_details, tm.new_text_id,
nullif(tm.new_text_id, tm.text_id) as next_text_id
FROM dbo.text_master_test tm
WHERE tm.text_id = @start_text_id
UNION ALL
SELECT tm.text_id, tm.text_details, tm.new_text_id,
nullif(tm.new_text_id, tm.text_id) as next_text_id
FROM dbo.text_master_test as tm
JOIN textHierarchy AS txtHr ON tm.text_id = txtHr.next_text_id
)
select text_id, text_details, new_text_id
from textHierarchy
);
go
select *
from dbo.textrecursion(1)
select *
from dbo.textrecursion(4)
select *
from dbo.textrecursion(5)
go
drop function dbo.textrecursion;
go
drop table dbo.text_master_test
go