需要帮助编写触发器以仅允许某些用户基于JOINED表进行更新/删除

时间:2011-10-20 21:51:43

标签: sql sql-server-2008

我有两张桌子,让我们称之为A& B,可以通过外键连接在一起。表A上有一个BIT字段(仅在表A 上),一旦设置为TRUE,我只希望某个组成员的用户能够更新/删除记录。

对于表A,我使用了以下内容:

CREATE trigger [trigger_TableA] on [TableA] after update, delete
as
begin
set nocount on
 if exists (select 1 from deleted d where d.[BitFlag] = 1)
     and exists (select 1 from inserted i where i.[BitFlag] = 1)
     and 1 != isnull(IS_MEMBER('GROUP_NAME'), 0)
 begin
     RAISERROR('Only members of GROUP_NAME are allowed to update or delete.', 16, 1)
     ROLLBACK TRAN
  RETURN
 end
end

如何将表的上述内容修改为表B的触发器,其中涉及检查表A上的BitFlag

谢谢!

1 个答案:

答案 0 :(得分:1)

 if exists (select 1 from deleted d INNER JOIN tableA a on d.? = a.? where a.[BitFlag] = 1) 
     and exists (select 1 from inserted i INNER JOIN tableA a on i.? = a.? where a.[BitFlag] = 1) 

当然,您必须用您需要加入的任何字段替换?s。