I want to get the current date time every time I update or insert a row in SQL Server

sql, sql-server

Solution

You can use TRIGGER The trigger will be automatically run when insert/update happen in the table.

For example:

create table tbl1
(
    col1 int primary key,
    col2 datetime
)
go
create trigger trg01
on tbl1
for insert, update
as 
begin
   update tbl1 
   set col2 = GETDATE()
   where col1 in (select col1 from inserted)
end   
go
insert into tbl1(col1) values (1);

Problem

I have a table `s_phone` in this table there is column `s_update`. I want to get the current date time every time I update or insert into this table `getdate()` or `systime()` or `CURRENT_TIMESTAMP` get the date time when insert only not when update

Original source