Jack S
Jack S

Reputation: 3

Create trigger error: 'Incorrect Syntax near 'dbo'

I'm currently learning some regarding SQL triggers. I am trying to create a column on a test table which will show the current date when the row is updated. The column datatype is type DateTime So far I have:

CREATE TRIGGER lastUpdated
ON [dbo].[ImportTest]
AFTER UPDATE
AS
BEGIN 
    IF NOT UPDATE(LAST_UPD)
    BEGIN
        SET [dbo].[ImportTest].[LAST_UPD] = CURRENT_TIMESTAMP
    END
END 
GO

However, I get the following error trying to execute:

Msg 102, Level 15, State 1, Procedure lastUpdated, Line 29
Incorrect syntax near 'dbo'.

Any help would be appreciated.

Upvotes: 0

Views: 1852

Answers (1)

Pரதீப்
Pரதீப்

Reputation: 93724

You cannot update a column like assigning value to variable. Try this

IF NOT UPDATE(LAST_UPD)
  BEGIN
      UPDATE IT
      SET    [LAST_UPD] = CURRENT_TIMESTAMP
      FROM   [dbo].[ImportTest] IT
      WHERE  EXISTS (SELECT 1
                     FROM   inserted i
                     WHERE  i.somecol = it.somecol
                     And ...)
  END 

Upvotes: 1

Related Questions