开发者

how to find users roles stored using bitwise technique in sql?

开发者 https://www.devze.com 2023-04-07 12:04 出处:网络
I would like to run a simple query to get all users of a particular role, the problem is user roles are开发者_JS百科 stored in bitwise numbers using power of 2 pattern.

I would like to run a simple query to get all users of a particular role, the problem is user roles are开发者_JS百科 stored in bitwise numbers using power of 2 pattern.

For example the roles table is.

1      role one
2      role two...
4
8
16

In a user table we have

username  other columns   roles
  billy       ....        192949

I'm not sure how to query for a role like that...


For a single role:

DECLARE @RoleOne = 1

SELECT * FROM Users
WHERE (roles & @RoleOne) > 0

Or a member of all multiple roles

DECLARE @MultipleRoles = 1 + 4 + 64

SELECT * FROM Users
WHERE (roles & @MultipleRoles) = @MultipleRoles

(I'm assuming SQL Server but will be very similar for other flavours)

0

精彩评论

暂无评论...
验证码 换一张
取 消