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 field UpdatedAt
has to be updated every time an UPDATE is done on the record. 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 delete the record but it updates the field Deleted
but the Triggers do not work for me
I already solved it, so I leave it:
UPDATE
DELETE