Developer Developer - 5 months ago 11
SQL Question

How to select parent child till end using root parent id in sql?

Below the table :
I need to get without

recursion
, but use any other
join
and
union
.

Id ParentId
1 0
2 1
3 2
4 2
5 4
6 5
7 6
8 7
9 8
N N


Without
recursion
use any other
join
queries

ruk ruk
Answer
DECLARE @Nvalue INT = 10

SELECT NUMBER AS ID,NUMBER-1 AS ParentID FROM (
SELECT DISTINCT NUMBER FROM master..spt_values WHERE number BETWEEN 1 AND @Nvalue
)T