0

我有以下情况,我不确定如何最好地解决它。任何有关如何准备所需视图的指导将不胜感激。

我有 4 个表:
用户 (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

注意:这里的示例数据只有一个“系统管理员”用户,但可以有任意数量的这种类型的用户。

4

2 回答 2

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
于 2013-08-12T04:36:23.650 回答
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
于 2013-08-12T05:43:37.490 回答