0

我有一个角色和权限表,其中包含一个名为 role_permissions 的连接表。

对于每个角色,如果该角色尚未拥有该权限,我想插入一个新权限。我的循环的问题是只有一行(角色)被更新。该循环似乎没有正确地重新评估每个循环上的子查询(它找到第一个没有权限的角色)

DECLARE @role_id INT

SET @role_id = (SELECT TOP 1 role_id FROM p_role_permissions WHERE NOT EXISTS (SELECT NULL FROM p_role_permissions WHERE permission_id = 57))

WHILE @role_id IS NOT NULL
BEGIN

INSERT INTO p_role_permissions (role_id, permission_id) VALUES(@role_id, 57)
-- does not appear to evaluate correctly (works only on the first loop)
SET @role_id = (SELECT TOP 1 role_id FROM p_role_permissions WHERE NOT EXISTS (SELECT NULL FROM p_role_permissions WHERE permission_id = 57))

END
4

4 回答 4

0

好的,在这里做一些假设,但我相信你可以这样做:

获取没有权限 57 的角色:

INSERT INTO p_role_permissions (role_id, permission_id)
   SELECT
       rp.Id,
       57
   FROM
       (SELECT
            r.Id
         FROM roles as r
         LEFT OUTER JOIN p_role_permissions as rp
         ON r.Id = rp.Role_Id AND rp.Permission_Id = 57

         WHERE r.Id IS NULL) as nr
于 2012-09-07T12:46:24.793 回答
0
INSERT INTO p_role_permissions (role_id, permission_id) (SELECT role_id, 57 FROM p_role_permissions WHERE NOT EXISTS (SELECT NULL FROM p_role_permissions WHERE permission_id = 57))
于 2012-09-07T12:46:53.290 回答
0

你不需要循环。您可以在一个插入语句中执行此操作。

;WITH RolesToAdd
     AS (SELECT role_id
         FROM   p_role_permissions /*Would be better to use a roles table here */
         EXCEPT
         SELECT role_id
         FROM   p_role_permissions
         WHERE  permission_id = 57)
INSERT INTO p_role_permissions
            (role_id,
             permission_id)
SELECT role_id,
       57
FROM   RolesToAdd 
于 2012-09-07T12:45:22.413 回答
0
MERGE INTO p_role_permissions 
USING (SELECT DISTINCT roleid, 57 FROM p_role_permissions) AS src  
ON (p_role_permissions)  
WHEN NOT MATCHED THEN   
    INSERT p_role_permissions (role_id, permission_id)
    VALUES (role_id, 57);  
于 2012-09-07T14:25:16.040 回答