Skip to content

Instantly share code, notes, and snippets.

@dreams-money
Last active March 12, 2025 19:21
Show Gist options
  • Select an option

  • Save dreams-money/56d2dc614cf77c16e77848b9efffd3e6 to your computer and use it in GitHub Desktop.

Select an option

Save dreams-money/56d2dc614cf77c16e77848b9efffd3e6 to your computer and use it in GitHub Desktop.
Microsoft SQL - Add an auto update clause to a column (MSSQL)
create table testing
(
id int primary key clustered,
value varchar(50) not null,
updated_at datetime2
);
insert into testing values
(1, 'foo', null),
(2, 'bar', null);
select * from testing;
CREATE TRIGGER tgr_updated_at
on testing
after update as
update testing
set updated_at = GETDATE()
where id in (select id from inserted);
select * from testing;
update testing set value = 'biz' where id = 1;
select * from testing;
drop trigger tgr_updated_at;
drop table testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment