创建触发器以在添加新记录时更新日期SQL Server

x6yk4ghg  于 2022-11-28  发布在  SQL Server
关注(0)|答案(1)|浏览(172)

编辑:我把我的问题解释得不正确。手头的问题-
在Appointment表上创建一个触发器,该触发器将在每次向Appointment表中添加新记录时更新Patient表上的LastContactDate。LastContactDate的值应为添加记录的日期。
如何创建触发器来更新LastContactDate列,以便在每次向Appointment表中添加新记录时记录日期?
这是我目前拥有的。

CREATE TRIGGER tr_Appointment_AfterInsert
ON Appointment
AFTER INSERT
AS
BEGIN
    INSERT INTO Appointment
    SET LastContactDate = GETDATE()
    FROM Appointment o
    INNER JOIN Inserted i
        ON o.AppDate = i.AppDate
        AND o.AppStartTime = i.AppStartTime
END

你能帮我修复这个代码吗?

mrphzbgm

mrphzbgm1#

该查询应该就是您要搜索的内容。

CREATE TRIGGER tr_Appointment_AfterInsert
ON Appointment
AFTER INSERT
AS
BEGIN
    UPDATE p
    SET LastContactDate = GETDATE()
    FROM Inserted i
    INNER JOIN Patient p
        -- Here the condition between Patient and Appointment (Inserted has the same columns and the new row of Appointment)
        ON ...
END

相关问题