D
D
dienerie2022-04-08 10:07:00
SQL Server
dienerie, 2022-04-08 10:07:00

How to automatically set updated_at and data_create?

It is necessary to automatically record the update date when changing the line, and also, during the initial unloading, record the date of creation. So I understand, something in default value to write down or the trigger? How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Tsvetkov, 2022-04-08
@dienerie

And this and that:

CREATE TABLE TestGETDATE	(
  [ID] [int] IDENTITY(1,1) NOT NULL  PRIMARY KEY,
  [Value] VARCHAR (50) NULL,
  [Data_create] DATETIME NULL DEFAULT GETDATE(),
  [Updated_at] DATETIME NULL)
GO

CREATE OR ALTER TRIGGER TestGETDATEUpdate ON TestGETDATE FOR UPDATE AS 
BEGIN
  UPDATE TestGETDATE
    SET Updated_at = GETDATE()
    WHERE ID IN ( SELECT ID FROM Inserted )
END
GO

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question