-
-
Save kzelda/7b1b669276d518400739c62f00c51159 to your computer and use it in GitHub Desktop.
SQL Server - add createdAt and updatedAt columns, automatically updates
This file contains 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
ALTER TABLE myTable | |
add createdAt datetime | |
CONSTRAINT DF_myTable_createdat DEFAULT GETDATE() | |
ALTER TABLE myTable | |
add updatedAt datetime | |
CONSTRAINT DF_myTable_updatedAt DEFAULT GETDATE() | |
go | |
create trigger trg_myTable_update on myTable for update as | |
begin | |
update myTable | |
set updatedAt = getDate() | |
from myTable inner join deleted d | |
on myTable.id=d.id | |
end | |
go |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment