CREATE OR ALTER TRIGGER TR_Agenda_GerarCodigo
ON Agenda
AFTER INSERT
AS
BEGIN
    SET NOCOUNT ON;

    UPDATE A
    SET CODIGO = 'AG' + RIGHT('0000' + CAST(A.ID_AGENDA AS VARCHAR), 4)
    FROM Agenda A
    INNER JOIN inserted I ON A.ID_AGENDA = I.ID_AGENDA;
END
GO