Recrusive sp

  • I have a table 2 fields only:

    group

    member

    It is possible that a gorup can be a member as well. Eg:

    gp A  member1

    gpA   gpB

    gpA   member2

    gpB   member3

    I need to report all the nested members. In the above case:

    gpA  has the following members:

    member1, member2, member3

    I am looking towards a recrusive SP where is all the nested members are returned.

     

    Any advise ?      

     

     

     

  • Can you post your data definition language (DDL)? From that we can help you build the recursive query properly. Also, is this for SQL Server 2000 or SQL Server 2005?

    K. Brian Kelley
    @kbriankelley

  • Would This Work:

    DECLARE @grp VARCHAR(50)

    SELECT MEM

    FROM  GRPMEM

    WHERE  (GRP LIKE @grp OR GRP IN (

    SELECT GRP

    FROM GRPMEM

    WHERE GRP IN (SELECT MEM FROM GRPMEM WHERE GRP LIKE @grp)

    )) AND MEM NOT IN (SELECT DISTINCT GRP FROM GRPMEM)

  • Here is the schema and sample data:

    usrGrps

    id           int

    Name         char(1000)

    member       char(1000)

    1   telecom    chan1

    2   telecom    gp1

    3   telecom    gp2

    4   gp1        ell1

    5   gp1        gp3

    Id is unique

  • hi ramses2nd

    I checked the data, the nesting can go upto 4/5 levels. So the I need to have as many inner select distinct.

     

  • How do you tell the difference between users and groups?

    K. Brian Kelley
    @kbriankelley

  • Also this is sqlserver 2000.

    Thanks

  • If a user/member does not have users within itself than this is a group.

     

     

  • Found a great article, my data and requirements are similar:

    http://support.microsoft.com/default.aspx?scid=kb;en-us;q248915

  • This is a good article. I was working up an example query based on your exact table, just haven't had time due to too many things going on at work.

    K. Brian Kelley
    @kbriankelley

  • Appreciate your time Brian, I knew this was a challange.

  • Well, I've written them before and actually, I just looked at subject for an article I completed in the last week. If it sees print, it'll be in SQL Server Standard. SQL Server 2005 has what is known as Common Table Expressions (CTEs) and they greatly simplify recursive solutions.

    K. Brian Kelley
    @kbriankelley

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply