简体   繁体   中英

Need help writing trigger to only allow certain users to update/delete based on JOINED tables

I have two tables, let's just call them A & B, which can be joined together via a foreign key. There is a BIT field on Table A (and only on Table A ), and once set to TRUE, I want ONLY users of a certain group membership to able to update/delete the record.

For just Table A, I used the following:

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

How would I modify the above for table to be a Trigger for Table B that involves checking the BitFlag on Table A?

Thanks!

 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) 

Of course you will have to replace the ?s with whatever field you need to join on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM