我有以下情况,我不确定如何最好地解决它。任何关于如何准备所需视图的指导将不胜感激。
我有4张桌子:
users(userid int,username varchar)
角色(roleid int,rolename varchar)
businessunit(buid int,buname varchar)
user_role_map(userid,roleid,buid)
在角色表中,我有一个id为0的角色,即“系统管理员”角色,在businessunit表中,我有一个IT业务部门。由以下查询产生的任何用户都将被视为系统管理员,并且应具有对每个业务单位的完全访问权限。
SELECT userid FROM user_role_map WHERE roleid = 0 AND buid = 0
我需要构建一个视图,显示所有“非系统管理员”联合到每个业务部门和每个“系统管理员”用户的列表。第一部分很简单,下面的查询,但第二部分是我正在努力的。
SELECT userid, roleid, buid FROM user_role_map WHERE roleid > 0 AND buid > 0
我将提供一些示例数据来帮助说明我想要实现的目标:
users
---------------
1, "sysAdmin"
2, "salesUser1"
3, "serviceUser1"
4, "manager1"
5, "salesUser2"
6, "serviceUser2"
7, "manager2"
roles
---------------
0, "SystemAdmin"
1, "Full"
2, "Update"
3, "Read"
businessunit --------------- 0,“IT” 1,“fooSales” 2,“fooService” 3,“barSales” 4,“barService”
user_role_map
---------------
1, 0, 0
2, 1, 1
2, 3, 3
3, 1, 2
3, 3, 4
4, 1, 1
4, 1, 2
5, 1, 3
5, 3, 1
6, 1, 4
6, 3, 3
7, 1, 2
7, 1, 4
最后,我需要视图为上面的示例数据提供以下内容(注意最后4行):
new view
---------------
2, 1, 1
2, 3, 3
3, 1, 2
3, 3, 4
4, 1, 1
4, 1, 2
5, 1, 3
5, 3, 1
6, 1, 4
6, 3, 3
7, 1, 2
7, 1, 4
1, 1, 1
1, 1, 2
1, 1, 3
1, 1, 4
注意:此处的示例数据只有一个“系统管理员”用户,但可以有任意数量的此类用户。
答案 0 :(得分:0)
您可以将业务单位的UNION ALL附加到您的视图中。
SELECT
userid,
roleid,
buid
FROM
user_role_map
WHERE
roleid > 0
AND buid > 0
UNION ALL
/* append full control for system admins to all bussiness units */
SELECT
CAST(1 AS INT) AS userid,
CAST(1 as INT) AS roleid,
BU.buid
FROM businessunit BU
答案 1 :(得分:0)
你应该可以这样做:
declare @users table(userid int, username varchar(255));
insert into @users values (1, 'sysAdmin');
insert into @users values (2, 'salesUser1');
insert into @users values (3, 'serviceUser1');
insert into @users values (4, 'manager1');
insert into @users values (5, 'salesUser2');
insert into @users values (6, 'serviceUser2');
insert into @users values (7, 'manager2');
declare @roles table(roleid int, rolename varchar(255));
INSERT INTO @roles VALUES (0, 'SystemAdmin');
INSERT INTO @roles VALUES (1, 'Full');
INSERT INTO @roles VALUES (2, 'Update');
INSERT INTO @roles VALUES (3, 'Read');
DECLARE @user_role_map TABLE(userid INT, roleid INT, buid int)
INSERT INTO @user_role_map values (1, 0, 0);
INSERT INTO @user_role_map values (2, 1, 1);
INSERT INTO @user_role_map values (2, 3, 3);
INSERT INTO @user_role_map values (3, 1, 2);
INSERT INTO @user_role_map values (3, 3, 4);
INSERT INTO @user_role_map values (4, 1, 1);
INSERT INTO @user_role_map values (4, 1, 2);
INSERT INTO @user_role_map values (5, 1, 3);
INSERT INTO @user_role_map values (5, 3, 1);
INSERT INTO @user_role_map values (6, 1, 4);
INSERT INTO @user_role_map values (6, 3, 3);
INSERT INTO @user_role_map values (7, 1, 2);
INSERT INTO @user_role_map values (7, 1, 4);
DECLARE @businessunit TABLE(buid int, buidname VARCHAR(255));
INSERT INTO @businessunit VALUES (0, 'IT')
INSERT INTO @businessunit VALUES (1, 'fooSales')
INSERT INTO @businessunit VALUES (2, 'fooService')
INSERT INTO @businessunit VALUES (3, 'barSales')
INSERT INTO @businessunit VALUES (4, 'barService')
--non-admin users
SELECT userid, roleid, buid
FROM @user_role_map
WHERE
roleid > 0 AND buid > 0
UNION ALL
--get admin users and add a full control entry
SELECT userid, 1, BusinessUnits.buid
FROM @user_role_map m
CROSS JOIN(
--use this if you have a businessunit table you can leverage; otherwise,
--you can select distinct buid on role_map where buid > 0
SELECT buid
FROM @businessunit
WHERE buid > 0
) AS BusinessUnits
WHERE
roleid = 0 AND m.buid = 0