How can I do a recursive self-join in SQL Server ? I have a table like this:
TableID | ParentID 1 | NULL 2 | 1 3 | 1 4 | 3 5 | NULL 6 | 4 7 | 6
I want to get the following results based on given TableID
to get all the ParentsID
related to the TableID
, let's say I want to get all the parents for the TableID = 6
:
TableID 6 4 3 1
I'm stuck on this and i don't know how to get the result in SQL Query ... Hope to tell me the SQL Query to get the previous data