Example Database
id master_id ref_id
1 p1 c1
2 p2 c1
3 c1 c3
4 c3 c1
5 p3 c4
6 p3 c5
Starting with "p1" I wish to return the recursive list of master_id:ref_id
Here's the problem:
p1:c1
c1:c3
c3:c1 (circular reference)
c1:c3
.....
.....
etc
How do I overcome the circular reference using sql (T-SQL or CTE recursion)