T-SQL Trigger Update

后端 未结 3 1483
暗喜
暗喜 2020-12-16 12:56

I have a table with 3 fields [ID, Name, LastUpdated].
LastUpdated has a default value of \"GetDate() so it automatically fills itself when a new record is added.

相关标签:
3条回答
  • 2020-12-16 13:43
    CREATE TRIGGER dbo.refreshModifyDate 
          ON  tStoreCategoriesImages
          FOR INSERT, UPDATE
    AS 
      BEGIN
        SET NOCOUNT ON;
            update t set t.ModifyDate = getdate() from tStoreCategoriesImages t 
            inner join inserted i on i.ID = t.ID
      END
    GO
    
    0 讨论(0)
  • 2020-12-16 13:53
    Update table1
    set LastUpdated = getdate()
    from inserted i, table1 a
    where i.pk1 = a.pk1
    
    0 讨论(0)
  • 2020-12-16 14:01

    From 'INSERTED', table INSERTED is common to both the INSERT, UPDATE trigger.

    CREATE TRIGGER dbo.Table1_Updated
    ON dbo.Table1
    FOR INSERT, UPDATE /* Fire this trigger when a row is INSERTed or UPDATEd */
    AS BEGIN
      UPDATE dbo.Table1 SET dbo.Table1.LastUpdated = GETDATE()
      FROM INSERTED
      WHERE inserted.id=Table1.id
    END
    
    0 讨论(0)
提交回复
热议问题