Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update one column in a trigger after updating only particular column in the same table in SQL Server 2008

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.

like image 298
amal50 Avatar asked May 04 '15 19:05

amal50


People also ask

How we can fire trigger for update on particular column?

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.

Can we create more than one trigger of same event on same table?

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.

Can we update multiple columns in a single update statement in SQL?

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.


2 Answers

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
like image 146
FutbolFan Avatar answered Oct 15 '22 04:10

FutbolFan


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
like image 21
marc_s Avatar answered Oct 15 '22 04:10

marc_s