-2

I have DB and i insert createdTime col with getDate and it work fine. now i want to add col "Update time", how i do it? Thanks!!!

Baruch Mashasha
  • 951
  • 1
  • 11
  • 29
  • 3
    You need a trigger for `updatetime` in SQL Server (or you need to set the value explicitly in the `update` statement). – Gordon Linoff Sep 16 '19 at 12:42

2 Answers2

1

As @Gordon Linof stated by commenting your question, you can achieve this by using a trigger.

Here you can find practical example:

CREATE TRIGGER [dbo].[xxx_update] ON [dbo].[MYTABLE]
FOR UPDATE
AS
BEGIN

    UPDATE MYTABLE
    SET mytable.CHANGED_ON = GETDATE()
        ,CHANGED_BY = USER_NAME(USER_ID())
    FROM inserted
    WHERE MYTABLE.ID = inserted.ID

END

Source: SQL Server after update trigger

Anyhow, i would prefer to let the application update the CHANGED_ON column while running the update statement.

B3S
  • 1,021
  • 7
  • 18
0

I found easy way to do this:

1.Add col dataModified to you table

2.Write this code

CREATE TRIGGER trg_update_my_table on my_table FOR UPDATE AS            
BEGIN
    UPDATE my_table
    SET dataModified=getdate()
    FROM my_table INNER JOIN deleted d
    on my_table.id = d.id
END
Baruch Mashasha
  • 951
  • 1
  • 11
  • 29