在更新后的触发器中插入更改日志。

时间:2022-09-28 22:56:20

I need to code an AFTER UPDATE trigger. When the pay rate of the employees table is modified. It needs to take that information and create a line in the ChangeLog table. My tables are below:

我需要编写一个AFTER UPDATE触发器。当员工表的工资率被修改时。它需要获取该信息并在ChangeLog表中创建一行。我的表如下:

Employees:

员工:

EmployeeID INT (PK),
PayPerHour MONEY

ChangeLog:

更新日志:

ChangeID INT (PK),
EmpID (FK to EmployeeID),
ChangedBy NVARCHAR(30),
DateChanged SMALLDATETIME,
OldRate MONEY,
NewRate MONEY

Here's what I put together. There's no error but it's not creating lines in the ChangeLog. (I was under the impression that inserted = new data, deleted = old data). Ideally, the EmpID would match, ChangedBy would be the System user, DateChanged would be a timestamp, OldRate would be the pre-updated rate, and NewRate would be the post-updated rate.

这是我加起来的。没有错误,但是它没有在ChangeLog中创建行。(我的印象是插入=新数据,删除=旧数据)。理想情况下,EmpID将匹配,change edby将是系统用户,DateChanged将是时间戳,OldRate将是预更新速率,NewRate将是后更新速率。

CREATE TRIGGER PayRate_UPDATE
ON Employees
AFTER UPDATE
AS
BEGIN
DECLARE @EmpID INT
SELECT @EmpID = EmployeeID
FROM inserted

DECLARE @OldRate MONEY
SELECT @OldRate = PayPerHour
FROM deleted

DECLARE @DateChanged SMALLDATETIME
SELECT @DateChanged = GETDATE()

DECLARE @ChangedBy NVARCHAR(128)
SELECT @ChangedBy = suser_sname()

DECLARE @NewRate MONEY
SELECT @NewRate = PayPerHour
FROM inserted

IF UPDATE(PayPerHour)
    UPDATE ChangeLog
        SET EmpID = @EmpID,
            ChangedBy = @ChangedBy,
            DateChanged = @DateChanged,
            OldRate = @OldRate,
            NewRate = @NewRate
END;

1 个解决方案

#1


4  

You have a couple of problems in your code:

你的代码有几个问题:

  1. Triggers must handle a set of updates (not a single update)
  2. 触发器必须处理一组更新(不是单个更新)
  3. Your trigger does an UPDATE not an INSERT to your ChangeLog
  4. 您的触发器执行更新,而不是对ChangeLog进行插入

Your code should look more like:

您的代码应该看起来更像:

CREATE TRIGGER PayRate_UPDATE ON Employees
AFTER UPDATE
AS
 BEGIN

    DECLARE @DateChanged SMALLDATETIME
    SELECT  @DateChanged = GETDATE()

    DECLARE @ChangedBy NVARCHAR(128)
    SELECT  @ChangedBy = SUSER_SNAME()

  IF UPDATE(PayPerHour) 
  INSERT  [ChangeLog]
          (
            EmpID,
            ChangedBy,
            DateChanged,
            OldRate,
            NewRate
          )
          SELECT
            i.EmployeeID,
            @ChangedBy,
            @DateChanged,
            d.PayPerHour,
            i.PayPerHour
          FROM
            INSERTED i
            LEFT JOIN DELETED d
              ON i.EmployeeID = d.EmployeeID

  END;

So, The revised trigger can now handle the situation where everyone gets a pay rise such as

因此,修改后的触发器现在可以处理每个人加薪的情况,比如

UPDATE Employees SET PayPerHour = PayPerHour  * 1.1 -- everyone gets a 10% payrise YAY!

And Your changelog does keeps a history of every update to each employees pay rate.

而且你的changelog确实保存了每个员工的每一次更新的历史记录。

#1


4  

You have a couple of problems in your code:

你的代码有几个问题:

  1. Triggers must handle a set of updates (not a single update)
  2. 触发器必须处理一组更新(不是单个更新)
  3. Your trigger does an UPDATE not an INSERT to your ChangeLog
  4. 您的触发器执行更新,而不是对ChangeLog进行插入

Your code should look more like:

您的代码应该看起来更像:

CREATE TRIGGER PayRate_UPDATE ON Employees
AFTER UPDATE
AS
 BEGIN

    DECLARE @DateChanged SMALLDATETIME
    SELECT  @DateChanged = GETDATE()

    DECLARE @ChangedBy NVARCHAR(128)
    SELECT  @ChangedBy = SUSER_SNAME()

  IF UPDATE(PayPerHour) 
  INSERT  [ChangeLog]
          (
            EmpID,
            ChangedBy,
            DateChanged,
            OldRate,
            NewRate
          )
          SELECT
            i.EmployeeID,
            @ChangedBy,
            @DateChanged,
            d.PayPerHour,
            i.PayPerHour
          FROM
            INSERTED i
            LEFT JOIN DELETED d
              ON i.EmployeeID = d.EmployeeID

  END;

So, The revised trigger can now handle the situation where everyone gets a pay rise such as

因此,修改后的触发器现在可以处理每个人加薪的情况,比如

UPDATE Employees SET PayPerHour = PayPerHour  * 1.1 -- everyone gets a 10% payrise YAY!

And Your changelog does keeps a history of every update to each employees pay rate.

而且你的changelog确实保存了每个员工的每一次更新的历史记录。