January 25, 2006 at 12:25 pm
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 ?
January 25, 2006 at 1:14 pm
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
January 25, 2006 at 1:34 pm
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)
January 25, 2006 at 1:57 pm
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
January 25, 2006 at 2:15 pm
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.
January 25, 2006 at 2:23 pm
How do you tell the difference between users and groups?
K. Brian Kelley
@kbriankelley
January 25, 2006 at 2:48 pm
Also this is sqlserver 2000.
Thanks
January 26, 2006 at 6:55 am
If a user/member does not have users within itself than this is a group.
January 26, 2006 at 10:25 am
Found a great article, my data and requirements are similar:
http://support.microsoft.com/default.aspx?scid=kb;en-us;q248915
January 26, 2006 at 11:23 am
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
January 26, 2006 at 12:05 pm
Appreciate your time Brian, I knew this was a challange.
January 26, 2006 at 12:49 pm
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