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 8708761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:12:49+00:00 2026-06-13T04:12:49+00:00

SQL gurus I have a table structure as follows Id Name IdPartner Id1 name1

  • 0

SQL gurus

I have a table structure as follows

Id        Name        IdPartner
Id1       name1           
Id2       Name2       Id1
Id3       name3       Id1
Id4       name4       Id2
Id5       name5       Id3

I need to write a query that would recursively loop through the table to find the partners.

I.e Given an Id say Id1 here, I need to pull out all the Names where Id1 is listed as a partner, So my query need to pull up Name3 and Name2. Subsequently in the same query I also need to pull the names where Name3 and Name2 are also listed as partners. So in this case if I give the input to the SP as Id1 I should effectively get a list that read as below

   Id          Name
  Id2         name2
  Id3         name3
  Id4         name4
  Id5         name5

I havent worked with recursively getting the data in SQL. Someone told me I could use a common table expression, but I fail to see how that would help me. I have been breaking my head over this for over a day and all I can come up with is the idea of using a cursor.

Any help in this regard helps

  • 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-06-13T04:12:50+00:00Added an answer on June 13, 2026 at 4:12 am

    The following returns the related, but not requested, rows:

    declare @Gurus as Table ( Id VarChar(4), Name VarChar(16), IdPartner VarChar(4) )
    insert into @Gurus ( Id, Name, IdPartner ) values
      ( 'Id1', 'name1', NULL ),
      ( 'Id2', 'Name2', 'Id1' ),
      ( 'Id3', 'Name3', 'Id1' ),
      ( 'Id4', 'Name4', 'Id2' ),
      ( 'Id5', 'Name5', 'Id3' )
    
    declare @TargetId as VarChar(4) = 'Id1'
    
    ; with RelatedGurus as (
      -- Anchor: Get the rows that are partners of the target row.
      select Id, Name, IdPartner
        from @Gurus
        where IdPartner = @TargetId
      union all
      -- Recursion: Add any rows that are partners to the rows just added.
      select G.Id, G.Name, G.IdPartner
        from @Gurus as G inner join
          RelatedGurus as RG on RG.Id = G.IdPartner )
      -- Display the result.
      select Id, Name
        from RelatedGurus
        order by Name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

a simple question for all you SQL gurus: I have the following table structures
Another one for you SQL Server gurus... I have a table like so... AccountManager
For you T-SQL gurus: I have the following table: ID Arrival 1 06:16:00 2
I have quick question for you SQL gurus. I have existing tables without primary
SQL Server 2005 I have an SQL Function (ftn_GetExampleTable) which returns a table with
i have few questions for SQL gurus in here ... Briefly this is ads
Ok SQL and Oracle gurus I have a somewhat complicated query that I'm trying
Alright SQL Server Gurus, fire up your analyzers. I have a list of titles
I have a table in SQL Server where, for each row r at time
SQL guru needed, this is killing my neurons. I have a db structure (simplified)

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.