Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

sql-server

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

like image 404
SHADLOVO Avatar asked Jan 23 '26 15:01

SHADLOVO


1 Answers

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);
like image 63
Iswanto San Avatar answered Jan 25 '26 07:01

Iswanto San



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!