Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6825021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:58:18+00:00 2026-05-26T21:58:18+00:00

I am trying to write a SQL query (for SQl Server), and am curious

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:58:19+00:00Added an answer on May 26, 2026 at 9:58 pm

    Using a recursive CTE it can look something like this:

    declare @T table
    (
      ColumnA varchar(10),
      ColumnB varchar(10)
    );
    
    insert into @T values  
    ('File 1',       'File 2'),
    ('File 2',       'File 3'),
    ('File 3',       'File 4'),
    ('File 7',       'File 13');
    
    with C as
    (
      select T.ColumnA,
             T.ColumnB,
             cast(T.ColumnA+','+T.ColumnB as varchar(max)) as Comb
      from @T as T
      where T.ColumnA = 'File 1'
      union all
      select T.ColumnA,
             T.ColumnB,
             C.Comb+','+T.ColumnB 
      from @T as T
        inner join C
          on T.ColumnA = C.ColumnB
    )
    select top 1 Comb
    from C
    order by len(Comb) desc
    option (maxrecursion 0)
    

    Result:

    Comb
    ------------------------
    File 1,File 2,File 3,File 4
    

    Default maxrecursion is 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a query for SQL Server 2005 but I can't
I'm trying to write a Microsoft SQL Server 2005 query that counts a total
I am trying to write a SQL Server query but have had no luck
I am trying to write my SQL Server 2008 query in such a way
I am trying to write a recursive CTE query in SQL Server 2005 but
I am trying to write a Microsoft SQL Server query for retrieving the oldest
I'm trying to write a recursive query in SQL Server that basically lists a
I'm trying to write a stored procedure in SQL Server that will eliminate some
I'm trying to write an SQL query that would search within a CSV (or
I am trying to write an SQL query within Visual Studio TableAdapter Query Wizard

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.