Search code examples
sql-serversql-server-2014sql-server-2014-express

SQL Server 2014 Express - Update trigger not executing


I installed SQL Server 2014 Express to test it a little (never used it before) and run into some difficulties,mainly with triggers.

First, I don't know why "new Trigger" under the tab Triggers (of the selected table) is grayed out.

Maybe because I have not used the table yet...don't know....

Well, I tried to do it with a query:

create trigger dbo.after_update 
on dbo.MyTable
for UPDATE
as begin
   if not update(edited)
      update dbo.Mytable 
      set edited = GETDATE()
      where TW_ID IN (SELECT TW_ID FROM inserted)
END
GO

I get the error :

Msg 8197, Level 16, State 4, Procedure after_update, Line 1
The object 'MyTable' does not exist or is invalid for this operation.

TW_ID is an identity column. Edited is smalldatetime.

There are no joined tables. All I want is to update the column edited when someone updates the table.


Solution

  • CREATE TRIGGER dbo.after_update 
    ON dbo.MyTable
    FOR UPDATE
    AS 
    BEGIN
      SET NOCOUNT ON;
    
        UPDATE t 
          SET t.edited = GETDATE()
        FROM dbo.Mytable t 
        INNER JOIN inserted I  ON t.TW_ID = I.TW_ID
    END
    GO