我的桌子是
ID | RoleID | PermissionName | CanRead | CanWrite | CanUpdate |
1 | 1 | Page1 | 1 | 0 | 1 |
2 | 1 | Page2 | 1 | 0 | 0 |
3 | 2 | Page1 | 0 | 1 | 0 |
4 | 2 | Page2 | 1 | 0 | 1 |
如果用户有2个角色,例如:RoleID 1,2
所以他有Page1的读取,写入,更新以及Page2 PermissionName的读取和更新
如果我将角色ID传递给存储过程如何获得组合结果,如下所示?
例如:
PermissionName | CanRead | CanWrite | CanUpdate |
Page1 | 1 | 1 | 1 |
Page2 | 1 | 0 | 1 |
并且用户可能有n个角色,根据他所拥有的角色列出每个PermissionName的组合权限
由于
答案 0 :(得分:2)
试试这个:
SELECT PermissionName,
CASE WHEN sum(CanRead) = 0 THEN 0 ELSE 1 END CanRead,
CASE WHEN sum(CanWrite) = 0 THEN 0 ELSE 1 END CanWrite,
CASE WHEN sum(CanUpdate) = 0 THEN 0 ELSE 1 END CanUpdate
FROM MyTable
WHERE ID = DesiredId
GROUP BY PermissionName
答案 1 :(得分:0)
您可以在存储过程中执行类似的操作
Declare @canRead int
Declare @canWrite int
Declare @canUpdate int
SELECT
Case canRead = 1 then @canread = @canRead + 1
END canRead
FROM
yourtablename
WHERE
id = desiredID
SELECT
permissionName,
Case @canReasd >= 1 THEN 1 ELSE 0 END canRead
FROM yourTableName
WHERE id = desiredID