📅  最后修改于: 2023-12-03 15:20:14.858000             🧑  作者: Mango
A "modify_timestamp" trigger in SQL Server is a trigger that automatically updates a specific column in a table with the current timestamp whenever a row in the table is modified or updated. This can be useful for tracking changes to a table and can provide valuable information about when modifications were made.
Create a trigger on the table you want to track changes on. The trigger should fire after an update is performed on the table. In the trigger, update the specified column with the current timestamp using the GETDATE()
function.
CREATE TRIGGER trigger_name
ON table_name
AFTER UPDATE
AS
BEGIN
UPDATE table_name
SET modify_timestamp = GETDATE()
FROM inserted
WHERE inserted.primary_key = table_name.primary_key
END
In the above trigger, trigger_name
is the name of the trigger, table_name
is the name of the table you want to track changes on, modify_timestamp
is the name of the column you want to update with the timestamp, and primary_key
is the primary key of the table.
The inserted
table is a temporary table that contains the updated rows that are being inserted into the main table. By joining this table with the main table using the primary key, you can update the modify_timestamp
column with the current timestamp for the updated rows.
The "modify_timestamp" trigger in SQL Server can be a powerful tool for tracking changes to a table. By automatically updating a specific column with the current timestamp, you can keep track of when modifications were made and better understand how your data is changing over time.