I have the following table with this structure:
CREATE TABLE [dbo].[Tasks]
(
[TasksID] [int] IDENTITY(1,1) NOT NULL,
[CommitteeID] [int] NULL,
[TransactionDateTime] [datetime2](7) NULL,
[inspectionStatus] [nvarchar](50) NULL,
[Latitude] [nvarchar](50) NULL,
[Longitude] [nvarchar](50) NULL,
[acceptanceState] [nvarchar](50) NULL,
[comments] [nvarchar](350) NULL,
[ScheduledDateTime] [datetime2](7) NULL,
)
What I want exactly is to create a trigger that update [TransactionDateTime]
with the current datetime, only if the column [acceptanceState]
is updated.
I have created the following trigger
CREATE TRIGGER [dbo].[TransactionDateUpdate]
ON [dbo].[Tasks]
AFTER UPDATE
AS BEGIN
UPDATE dbo.Tasks
SET TransactionDateTime = GETDATE()
FROM INSERTED i
WHERE i.TasksID = Tasks.TasksID
END
The problem with this trigger, that it updates the column [TransactionDateTime]
but if I made change on any of the column in the table, and what I want is to update [TransactionDateTime]
only if the column [acceptanceState]
is changed/updated. Can I find any help? How to add the condition of updating [TransactionDateTime]
only if [acceptanceState]
is changed/updated?
I searched a lot for the similar problem but I didn't find exactly the same problem.
In SQL Server, you can create DML triggers that execute code only when a specific column is updated. The trigger still fires, but you can test whether or not a specific column was updated, and then run code only if that column was updated. You can do this by using the UPDATE() function inside your trigger.
You can create multiple triggers for the same subject table, event, and activation time. The order in which those triggers are activated is the order in which the triggers were created. Db2 records the timestamp when each CREATE TRIGGER statement executes.
We can update multiple columns by specifying multiple columns after the SET command in the UPDATE statement. The UPDATE statement is always followed by the SET command, it specifies the column where the update is required.
You just needed to add an IF UPDATE check and join deleted table in your trigger:
CREATE TRIGGER [dbo].[TransactionDateUpdate]
ON [dbo].[Tasks]
AFTER UPDATE
AS
BEGIN
IF UPDATE(acceptanceState) --add this line
UPDATE dbo.Tasks
SET TransactionDateTime = GETDATE()
FROM INSERTED i
JOIN DELETED d on i.TasksID = d.TasksID --add this line
WHERE i.TasksID = Tasks.TasksID
END
The best way to do this is to compare the Inserted
and Deleted
pseudo table. In an AFTER UPDATE
trigger, the Deleted
pseudo table contains the old values, while Inserted
contains the new ones. So if the Deleted.acceptanceState
is not the same as Inserted.acceptanceState
, then that column has been updated.
So you need to slightly extend your trigger like this:
CREATE TRIGGER [dbo].[TransactionDateUpdate]
ON [dbo].[Tasks]
AFTER UPDATE
AS BEGIN
UPDATE dbo.Tasks
SET TransactionDateTime = GETDATE()
FROM Inserted i
INNER JOIN Deleted d ON i.TasksID = d.TasksID
WHERE i.TasksID = Tasks.TasksID
AND i.acceptanceState <> d.acceptanceState
END
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With