Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
vbArch
Starting Member
5 Posts |
Posted - 2006-05-23 : 13:50:57
|
I have a link table between a user table and a lookup that contains the following dataID Role ----------- ----------- 92166 192166 1192167 792167 1192210 392210 792210 1192211 792211 1192212 792212 1192213 792213 1192213 3The Link table stores whether a user has a combination of rolea. I need to be able to ask for any given user IE 92210 has the roles 3 and 7 any additional roles are ok but they must have all of the combinations I am asking for. The role combinations can be anywhere from 1 to 40. So in one case i have to ask for roles (3,7) and in another (1,7,11) The only way I can figure out how to do this is a dynamic self join or by using a cursor (yuck). This is a legacy application and the requirement changed from roles being inclusive to exclusive and a table structure change is out of the question. There must be a better way. Any help would be appreciated.Thanks in advance |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-05-23 : 16:24:27
|
| Put the roles into a table and the query you want isselect idfrom tbljoin roleson tbl.role = roles.rolegroup by idhaving count(*) = (select count(*) from roles)You can use http://www.nigelrivett.net/SQLTsql/ParseCSVString.html or something similar to get the table of roles from a csv string.otherwiseselect idfrom tblwhere @roles like '%,' + convert(varchar(20),role) = ',%'group by idhaving count(*) = len(@roles) - len(replace(@roles,',','')) + 1==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-05-24 : 02:47:55
|
| select idfrom tblwhere @roles like '%,' + convert(varchar(20),role) + ',%'group by idhaving count(*) = len(@roles) - len(replace(@roles,',','')) + 1MadhivananFailing to plan is Planning to fail |
 |
|
|
vbArch
Starting Member
5 Posts |
Posted - 2006-05-24 : 12:34:41
|
| Thanks, I got it working, appreciate the help you saved me days |
 |
|
|
|
|
|
|
|