从删除的触发器中选择..查找谁删除了?

时间:2017-10-18 15:37:16

标签: sql sql-server

我创建了一个触发器,我想知道是否有办法找出谁删除了记录?

当我在触发器或USER_NAME()甚至USER中使用SUSER_NAME()时,我只获取SQL登录ID。触发器正在从具有我们可以自定义的.net后端的另一个软件激活。

USE JM_SB
GO

ALTER TRIGGER Sales_Update
ON Sales
FOR UPDATE
AS
BEGIN
   SET NOCOUNT ON;

   DECLARE @Action VARCHAR(30)
   DECLARE @UNAME VARCHAR(30)

   IF UPDATE(PartGroupID)
   BEGIN
          SET @Action = 'Updated Part Group'

          /* All this does is get dbo */
            Set @UNAME = (Select USER From Deleted)  

            INSERT INTO Sales_AuditTrail(Action, SalesID, OldValue, 
            NewValue, SalesDivision, UpdateDate, UpdatedUser)
            SELECT @Action, i.SalesID, d.PartGroupID, i.PartGroupID, 
            i.SalesDivision, Current_Timestamp As UpdateDate, @UNAME
            FROM Inserted i
            INNER JOIN Deleted d ON i.SalesID = d.SalesID

   END  
END;
GO

删除或插入表是否知道更新记录的人?而不是dbo或sa ...来自触发更新的软件我的用户名为ex:“jmadmin”

1 个答案:

答案 0 :(得分:0)

这是我使用的,它的工作原理。谢谢大家。

   DECLARE @cUserID As varchar(35)

   SET @cUserID = Upper(Left(App_Name(),20))