插入foreach x不存在的地方

时间:2012-07-12 18:12:13

标签: sql sql-server sql-server-2005 stored-procedures

我有一个相当简单的数据库,包含三个(相关)表:用户,权限和用户权限。基本前提很简单:创建用户时,权限表中的所有记录都会自动添加到user_permissions表中,并带有默认值。这一切都很好。

但是,由于我目前正在开发中,因此我继续添加新权限,当然现有用户将不会拥有这些权限,因为创建权限表时这些新权限不存在。所以,我有一个好主意,就是创建一个小的存储过程来自动更新user_permissions表,其中包含user_permissions表中当前不存在的所有权限。

因此,简而言之,我想做的就是(伪代码)

For each user without x permission in user_permissions, insert into user_permissions user_id and permission_id

我不太确定如何从SQL POV中执行此操作。我玩连接和“不存在”,但没有真正到达任何地方。

您可以在此处使用我的架构:http://sqlfiddle.com/#!3/b0761/3 在此先感谢您的帮助!

编辑:架构:

CREATE TABLE users (
  user_id      int IDENTITY(1, 1) NOT NULL,
  user_name    varchar(255),
  PRIMARY KEY (user_id));

CREATE TABLE permissions (
  permission_id          int IDENTITY(1, 1) NOT NULL, 
  permission_name        varchar(255) NOT NULL, 
  PRIMARY KEY (permission_id));

CREATE TABLE user_permissions (
  user_id       int NOT NULL, 
  permission_id int NOT NULL, 
  value         tinyint DEFAULT 0 NOT NULL,  
  PRIMARY KEY (user_id, 
  permission_id));

ALTER TABLE user_permissions ADD CONSTRAINT FK_user_pe338140 
FOREIGN KEY (permission_id)
REFERENCES permissions (permission_id);

ALTER TABLE user_permissions ADD CONSTRAINT FK_user_pe405324 
FOREIGN KEY (user_id) REFERENCES users (user_id);

INSERT INTO users(user_name) values('test_username');
INSERT INTO users(user_name) values('test_username2');

INSERT INTO permissions(permission_name) VALUES('permission_1')
INSERT INTO permissions(permission_name) VALUES('permission_2')

INSERT INTO user_permissions(user_id, permission_id, value)
VALUES(1, 1, 1)

INSERT INTO user_permissions(user_id, permission_id, value)
VALUES(2, 1, 1)

编辑:到目前为止查询

SELECT a.user_id, b.permission_id, 1 as 'value'
FROM USER_PERMISSIONS a right outer join 
PERMISSIONS b on a.permission_id = b.permission_id

4 个答案:

答案 0 :(得分:2)

insert into user_permissions (user_id, permission_id)
select
  u.user_id,
  p.permission_id
from
  users u
  cross join permissions p
where
  not exists (select 0 from user_permissions with (updlock, holdlock)
              where user_id = u.user_id and permission_id = p.permission_id)

参考:Only inserting a row if it's not already there

答案 1 :(得分:1)

INSERT dbo.user_permissions([user_id], permission_id, [value])
SELECT u.[user_id], p.permission_id, 1
FROM dbo.user_permissions AS u 
CROSS JOIN dbo.[permissions] AS p
WHERE NOT EXISTS (SELECT 1 FROM dbo.user_permissions 
  WHERE [user_id] = u.[user_id]
  AND permission_id = p.permission_id
)
GROUP BY [user_id], p.permission_id;

顺便说一句,你应该避免使用往往需要分隔符的名称,例如: user_idpermissions是关键字/保留字。

答案 2 :(得分:1)

曼斯菲尔德 如果我理解正确,您希望在添加新权限时为user_permissions表添加一个值。

我还假设您希望它默认为0.插入新权限后,运行此代码将为所有用户播放user_permissions表,默认值为0,表示当前未使用的任何权限。

--insert into permissions(permission_name) values('newperm');
--select * from permissions
insert into user_permissions(user_id, permission_id, value)
select
  u.user_id, p.permission_id, 0
from
             users u
  cross join permissions p
where
      p.permission_id not in(select permission_id from user_permissions)
;
--select * from user_permissions;

答案 3 :(得分:0)

以下查询将为您提供要插入的缺少的userpermission行:

select a.USER_ID,b.permission_id from users a,permissions b,user_permissions c
   where c.user_id <>a.user_id and c.permission_id <> b.permission_id