Back to Database
Database Trigger Implementation Guide
Database Engineering

Trigger Implementation
Guide.

Author

Upendra

Published

August 24, 2023

This trigger implementation guide focuses on automating expiry date calculations based on approval dates and predefined periods.

Trigger Functionality

When an update occurs in the table, the trigger is activated. It calculates the new Exdate based on the Approved Date plus the period specified in the No.of_days column.

The trigger updates the Exdate column in the table accordingly, ensuring synchronization between approval cycles and validity periods without manual intervention.

T-SQL Query Implementation

SQL Implementation
CREATE TRIGGER Trigger_Name
ON [Table]
AFTER UPDATE
AS
BEGIN
    -- Calculate the new expiry date based on the ApprovedDate + No.of_days
    UPDATE M
    SET ExDate = DATEADD(DAY, CAST(i.No.of_days AS INT), i.ApprovedDate)
    FROM [Table] M
    INNER JOIN inserted i ON M.[ID] = i.[ID]
    INNER JOIN Table1 T ON M.[TID] = T.[TbNumber]
    WHERE M.[TbNumber] = i.[TbNumber] AND i.[TID] = M.[TID];
END;