Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Please help


Please help

Author
Message
Prakash-485822
Prakash-485822
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 124
Dear All,

I have a two table Users && Roles

Roles contains following columns
Roleid, Rolename,InboxEnabled

Users contains
Userid,Region,Branchid,Roles

The Data in the Roles Table is
Admin -Admin -y
Enduser-EndUser-n
Dev -Developer-y
Network-Network-y

The data in the users table is
Ravi-North-Delhi-Admin,Dev,Network,
Raju-North-Delhi-Dev,Enduser,

select Roleid from Roles where Inboxenabled ='y' will return me Admin and Dev roles

when i pass this query as a condition to users table i have to get output as Ravi

Anyone please help me to solve this query
Andras Belokosztolszki
Andras Belokosztolszki
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1385 Visits: 1585
This is a good example of bad table design. You should not store the role membership in a single column as a list. Is this something you could change?

If not, then you could create a user defined function to check membership condition, you could use a condition like: Roles + ',' LIKE '%'+@therolename+',%', or a table valued function that returns a row for each of the role in a string, and use crossapply. The second of the above is the one that is the easiest to add quickly, but the best could be to fix your design.

Regards,
Andras



Andras Belokosztolszki, MCPD, PhD
GoldenGate Software
Prakash-485822
Prakash-485822
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 124
Thanks for your reply,

Is there any possiblity with any corelated query or subquery not by assinging to a variable
Adam Haines
Adam Haines
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2324 Visits: 3135

select Roleid from Roles where Inboxenabled ='y' will return me Admin and Dev roles

How will this only return Admin and Dev, if networking is 'y' in your test data? The returned data should be Admin, Dev, and networking.. correct?

I agree with Andras that table redesign is the best and most effiecient option. If you must use the current design, you can do something like this.


SELECT UserId
FROM users a INNER JOIN roles b ON a.roles LIKE '%' + b.roleid + '%'
WHERE b.Inboxenabled ='y'
GROUP BY UserId
HAVING COUNT(b.roleid) =
   (SELECT COUNT(RoleId)
    FROM @roles
    WHERE Inboxenabled ='y')





My blog: http://jahaines.blogspot.com
Prakash-485822
Prakash-485822
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 124
Thanks a lot it is working
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search