views:

136

answers:

2

Ok so I am writing a report against a third party database which is in sql server 2005. For the most part its normalized except for one field in one table. They have a table of users (which includes groups.) This table has a UserID field (PK), a IsGroup field (bit) , a members field (text) this members field has a comma separated list of all the members of this group or (if not a group) a comma separated list of the groups this member belongs to.

The question is what is the best way to write a stored procedure that displays what users are in what groups? I have a function that parses out the ids into a table. So the best way I could come up with was to create a cursor that cycles through each group and parse out the userid, write them to a temp table (with the group id) and then select out from the temp table?

UserTable
Example:
ID|IsGroup|Name|Members
1|True|Admin|3
2|True|Power|3,4
3|False|Bob|1,3
4|False|Susan|2
5|True|Normal|6
6|False|Bill|5

I want my query to show: GroupID|UserID
1|3
2|3
2|4
5|6

Hope that makes sense...

A: 

Your technique will probably be the best method.

mrdenny
Based on the fact that SQL Server doesn't parse text data very well. The only way to parse through a text field of an unknown length is via a loop or cursor.
mrdenny
That's what I was afraid of. Thanks for you help.
A: 
pflunk
Preliminary testing with this method looks good. I don't really care about performace since this query will be run a couple of times a quarter with less about a dozen groups and (maybe) twice that in users. Thanks for the help!