SET TERM ^ ;

CREATE OR ALTER procedure SPD_INTEGRACAO_MOD_DUPLICADA
as
declare variable COUNT_INI integer;
declare variable COUNT_IDS integer;
declare variable IDS_REMOTOS varchar(500);
declare variable MOD_ID_LOCAL integer;
BEGIN
    
    FOR SELECT
        X.MOD_ID_LOCAL,
        X.COUNT_IDS,
        X.IDS_REMOTOS
    FROM (
        SELECT
            T.MOD_ID_LOCAL,
            COUNT(T.ID) AS COUNT_IDS,
            LIST(T.ID)  AS IDS_REMOTOS
        FROM TBAPI_INTEGRACAO_MOD T
        WHERE T.API_INTEGRACAO_ID = 3
        GROUP BY T.MOD_ID_LOCAL
    ) X
    WHERE X.COUNT_IDS >= 2
    INTO :MOD_ID_LOCAL, :COUNT_IDS, :IDS_REMOTOS
    DO BEGIN

        :COUNT_INI = 1;
        WHILE(:COUNT_INI < :COUNT_IDS) DO
        BEGIN
            DELETE FROM TBAPI_INTEGRACAO_MOD T WHERE T.ID = FN_ARRAY(:IDS_REMOTOS, :COUNT_INI, ',');


            :COUNT_INI = :COUNT_INI + 1;
        END
    END
    
END
^

SET TERM ; ^


SET TERM ^ ;

CREATE OR ALTER procedure SPD_INTEGRACAO_PROD_DUPLICADA
as
declare variable COUNT_INI integer;
declare variable COUNT_IDS integer;
declare variable IDS_REMOTOS varchar(500);
declare variable PRO_ID_LOCAL integer;
declare variable TAMANHO integer;
BEGIN
    
    FOR SELECT
        X.PRO_ID_LOCAL,
        X.TAMANHO,
        X.COUNT_IDS,
        X.IDS_REMOTOS
    FROM (
        SELECT
            T.PRO_ID_LOCAL,
            T.TAMANHO,
            COUNT(T.ID) AS COUNT_IDS,
            LIST(T.ID)  AS IDS_REMOTOS
        FROM TBAPI_INTEGRACAO_PRO T
        WHERE T.API_INTEGRACAO_ID = 3
        GROUP BY T.PRO_ID_LOCAL, T.TAMANHO
    ) X
    WHERE X.COUNT_IDS >= 2
    INTO :PRO_ID_LOCAL, :TAMANHO, :COUNT_IDS, :IDS_REMOTOS
    DO BEGIN

        :COUNT_INI = 1;
        WHILE(:COUNT_INI < :COUNT_IDS) DO
        BEGIN
            DELETE FROM TBAPI_INTEGRACAO_PRO T WHERE T.ID = FN_ARRAY(:IDS_REMOTOS, :COUNT_INI, ',');


            :COUNT_INI = :COUNT_INI + 1;
        END
    END
    
END
^

SET TERM ; ^

