I have a table called City:
CREATE TABLE dbo.Ciudad(
Id varchar(36) NOT NULL,
DptId varchar(36) NOT NULL,
Nombre varchar(40) NOT NULL,
Version Timestamp NULL,
CreatedAt datetimeoffset NULL DEFAULT getdate(),
UpdatedAt datetimeoffset NULL,
Deleted bit NOT NUll DEFAULT(0),
CONSTRAINT [PK_Ciudad] PRIMARY KEY CLUSTERED (Id),
CONSTRAINT [FK_Ciudad_DptId] FOREIGN KEY (DptId) REFERENCES dbo.Dpto(Id)
)
The UpdatedAt
field must be updated each time an UPDATE is made in the registry. How can I do it with a Trigger?
At the moment I only have this:
UPDATE:
CREATE TRIGGER dbo.tr_Ciudad_Update ON dbo.Ciudad AFTER UPDATE
AS
DECLARE @id as VARCHAR
BEGIN
RAISERROR(@id ,16,-1)
UPDATE Ciudad SET UpdatedAt = getdate() WHERE Id = @id;
END
DELETE
CREATE TRIGGER dbo.tr_Ciudad_Eliminar ON dbo.Ciudad INSTEAD OF DELETE
AS
DECLARE @id as VARCHAR
BEGIN
UPDATE Ciudad SET UpdatedAt = getdate(), Deleted = 1 WHERE Id = @id;
RAISERROR('CAMPO DELETED ACTUALIZADO A TRUE',16,-1)
ROLLBACK TRANSACTION
END
The idea is that the delete does not erase the registry but I update the Deleted
field but the Triggers do not work for me