0

I have a SQL table like the following

RoleId      Roles
------------------------------------------
  1         Administrators Viewers Users Managers 
  2         Administrators Viewers Managers 
  3         Administrators
  4         Viewers  

I need to get the RoleId's based on the roles. I have each role as a string.

So when I use the following query

SELECT * 
FROM Roles 
WHERE Roles like '%Administrators Viewers Managers%'

I get only the first two rows

RoleId      Roles
--------------------------------------------------
  1         Administrators Viewers Users Managers 
  2         Administrators Viewers Managers 

But I need it to fetch the remaining rows too since it contains Administrators and Viewers. I am actually using an Stored procedure to supply the roles as a parameter. So it is not always the same. Its dynamic. I cant use OR. I need a solution that will work for any parameter.

Is there a combination between IN and LIKE clause ?

Or how can I proceed

4
  • 3
    hmmm this is an interested method, but doesnt seem like a good long-term solution. i think ideally you would have 2 tables which would allow you to be more flexible later... ie. Roles, and Permissions... ie. ROLES TABLE: roleID 1=Admin, roleID 2=Viewer PERMISSIONS TABLE: roleID=1, view=yes, admin=yes, edit=yes; roleId=2, view=yes, admin=no, edit=no. Then you would be able to do more complex queries. Commented Nov 25, 2014 at 10:18
  • How can you get row 1 with that query? Commented Nov 25, 2014 at 10:18
  • The script provided above would not bring back record 'Administrators Viewers Users Managers' as 'Users' is not specified in your LIKE clause. If your parameter is dynamic, I would suggest DSQL. Declare a parameter with writes out your query and execute. I am not sure how you are going to break your parameter up into 3 (Administrator / Viewers / Managers) variables though. Commented Nov 25, 2014 at 10:41
  • I think you need to do some research on relational databases. Your approach is wrong so unless you have a good reason to do it like that then you need to rethink it. Commented Nov 25, 2014 at 10:57

3 Answers 3

2

You can use OR:

SELECT * 
FROM Roles 
WHERE Roles like '%Administrators%'
OR    Roles like '%Viewers%'
Sign up to request clarification or add additional context in comments.

2 Comments

I am actually using an Stored procedure to supply the roles as a parameter. So it is not always the same. Its dynamic.
@Targarian: so what type is the parameter? I would normalize your table. Create another table to store the "Sub-Roles" which belong to a role.
0
SELECT *   FROM Roles 
WHERE Roles like '%Administrators%'  OR    Roles like '%Viewers%' 
OR Roles like '%Administrators Viewers%'

2 Comments

Could you add some additional information along with your answer?
Isn't the third LIKE completely redundant due to the first two?
0

If you dynamically pass the roles as a parameter use like the below Query for

SQL Server 2000 and above

SELECT * 
FROM Roles 
WHERE Roles like '%'+@Roles+'%'

SQL Server 2008 and above

SELECT * 
FROM Roles 
WHERE Contains(Roles,@Roles)

but the above query will produce the result only based on the roles you have passed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.