CREATE OR ALTER TRIGGER TR_CoordImpres_GerarCodigo
ON Coord_impres
AFTER INSERT
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @UltimoNumero INT;

    SELECT
        @UltimoNumero =
        ISNULL(
            MAX(
                TRY_CAST(
                    REPLACE(CODIGO,'CI','')
                AS INT)
            ),
        99)
    FROM Coord_impres
    WHERE CODIGO LIKE 'CI%';

    ;WITH Numerados AS
    (
        SELECT
            ID_COORD_IMPRES,
            ROW_NUMBER() OVER
            (
                ORDER BY ID_COORD_IMPRES
            ) AS Linha
        FROM inserted
    )
    UPDATE C
    SET
        CODIGO =
        'CI' +
        RIGHT(
            '0000' +
            CAST(
                @UltimoNumero + N.Linha
            AS VARCHAR),
        4)
    FROM Coord_impres C
    INNER JOIN Numerados N
        ON C.ID_COORD_IMPRES =
           N.ID_COORD_IMPRES;
END
GO