I am trying to write a SQL query (for SQl Server), and am curious if there is a simple method to achieve my ends. I will simplify things to get to the heart of the matter:
I have a table with two columns, which we can call column A and column B. Column A contains the name of a segmented file, and column B contains the next part of the segmented file. So, I can have multiple segments pointing to multiple segments.
So, for example:
Column A Column B
File 1 File 2
File 2 File 3
File 3 File 4
File 7 File 13
I need to get a list of all the segments starting with File 1 and ending with File 4, preferably in one column. Or, to put it another way, I need to start with a given entry in column A, and having something traverse and dereference entries from column B, until no entry can be found from column B in column A.
Something like SELECT allsegments() From FTable WHERE FTable.A = “File 1”? Or do you think I need to write some custom code?
Using a recursive CTE it can look something like this:
Result:
Default
maxrecursionis 100 so if you have more than 100 files in a chain you need to specify the number of allowed recursions.option (maxrecursion 0)makes the max number unlimited.