简体   繁体   中英

Create a trigger to update date when a new record is added SQL Server

EDITEDIT: I have explained my problem incorrectly. The question in hand -

Create a trigger on the Appointment table that will update LastContactDate on the Patient table each time a new record is added to the Appointment table. The value of the LastContactDate should be the date the record is added.

How do you create a trigger to update LastContactDate column to record the date every time a new record is added to an Appointment table?

This is what i currently have.

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
    

Could you help fix this code?

this query should be what you are searching for.

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

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