删除记录时在DELETE触发器中检索deletedBy

时间:2016-12-30 02:58:48

标签: sql-server

我有一张桌子。我使用触发器记录记录的变化。有一个字段名称" updatedBy"记录执行操作的人员(例如插入,更新,删除)

插入和更新很好,因为我将插入或更新字段以及其他参数,例如

INSERT INTO T (P1,UPDATEDBY) SELECT 'HELLO','ME'
UPDATE T SET P1 ='WORLD', UPDATEDBY = 'ME'

对于那些操作,我可以找到我的' ME'已插入/更新记录,以便我可以正确创建日志。但是,如何告诉数据库谁删除了记录?因为我刚刚执行

DELETE FROM T WHERE P1='WORLD'

感谢。

3 个答案:

答案 0 :(得分:0)

然后你还需要一个表来保存CRUD的活动日志(CReate,Update,Delete)

至少有3个参数

  • 名称
  • 日期
  • 动作

然后只需在执行CRUD操作时插入此表或至少仅删除大小写

答案 1 :(得分:0)

如果要更好地跟踪记录的更改,请使用SQL Server中提供的Magic Tables(已插入,已删除)。这两个表将包含有关插入,删除和更新记录的详细信息。

请点击链接以供参考,

http://www.c-sharpcorner.com/UploadFile/93126e/what-are-the-magic-tables-used-by-triggers-in-sql-server/

What are the magic tables available in SQL Server 2000?

或者如果您想接受当前用户

Getting current user with a sql trigger

答案 2 :(得分:0)

如果您要通过ado.net或实体框架中的应用程序进行删除,则无法跟踪谁删除了记录,您可以像我一样使用一个简单的技巧,并且只需进行最小的更改就可以很好地工作。表格中没有多余的列。

ALTER trigger [dbo].[CreateLogCart]
on [dbo].[Cart]
after UPDATE, INSERT, DELETE
as
declare @targetID bigint,
@user int, 
@activity varchar(10);
if exists(SELECT 1 from inserted) and exists (SELECT 1 from deleted)
begin
    SET @activity = 'UPDATE';
    **-- update createdby before delete and do not log**-----------------------------
    declare @iby int;
    declare @dby int;
    **-- update createdvy before delete and do not log**------------------------------
    SELECT @targetID = d.CartId, @dby = d.CreatedBy from deleted d;
    SELECT @user = d.modifiedby, @iby = d.CreatedBy from inserted d;

    if(@iby = @dby) **-- update createdby before delete and do not log** --------
    begin
      INSERT into AuditCart(CartId, UserId, AppId, Description, CreatedBy, CreatedOn, ModifiedBy, ModifiedOn, Activity, ActivityBy, ActivityOn)
    select @targetID, d.userid, d.AppId, d.Description, d.CreatedBy, d.CreatedOn, d.ModifiedBy, d.ModifiedOn, @activity, @user, getdate() from deleted d
    end

end

If exists (Select 1 from inserted) and not exists(Select 1 from deleted)
begin
    SET @activity = 'INSERT';
    SELECT @targetID = i.CartId from inserted i;
    SELECT @user = d.modifiedby from inserted d;
     INSERT into AuditCart(CartId, UserId, AppId, Description, CreatedBy, CreatedOn, ModifiedBy, ModifiedOn, Activity, ActivityBy, ActivityOn)
     select @targetID, d.userid, d.AppId, d.Description, d.CreatedBy, d.CreatedOn, d.ModifiedBy, d.ModifiedOn, @activity, @user, getdate() from inserted d
end

If exists(select 1 from deleted) and not exists(Select 1 from inserted)
begin 
    **-- revert createdby updated in delete operation**--------------
    declare @cby int;
     SET @cby = (select top 1 CreatedBy from dbo.AuditCart where Activity = 'INSERT' and CartId = (select d.CartId from deleted d) order by CreatedOn desc);  
    **-- revert createdby updated in delete operation**-------------

    SET @activity = 'DELETE';
    SELECT @targetID = i.CartId from deleted i;
    SELECT @user = d.CreatedBy from deleted d;
    INSERT into AuditCart(CartId, UserId, AppId, Description, CreatedBy, CreatedOn, ModifiedBy, ModifiedOn, Activity, ActivityBy, ActivityOn)
    select @targetID, d.userid, d.AppId, d.Description, ****@cby****, d.CreatedOn, d.ModifiedBy, d.ModifiedOn, @activity, @user, getdate() from deleted d
end

您只需要在删除之前更新createdby,并且不要将更新日志记录到更新触发器中(如果条件在更新触发器中显示), 现在,在删除触发器中,您只需将其值(在我的情况下为 activityby )以谁的值删除,就可以通过从旧的插入日志中创建它来恢复它。