Last active
March 12, 2025 19:21
-
-
Save dreams-money/56d2dc614cf77c16e77848b9efffd3e6 to your computer and use it in GitHub Desktop.
Microsoft SQL - Add an auto update clause to a column (MSSQL)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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