Delimiter //
/**
 * Author:  armen
 * Created: 8 nov. 2019
 */
Drop Procedure if Exists BUSINESS_ATTACHED_MEMBERS_INSERT_ACC_PROVIDED//
Create Procedure BUSINESS_ATTACHED_MEMBERS_INSERT_ACC_PROVIDED(
)
Begin
    SET @bamic_OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
    SET @bamic_OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;

    bamic_main: Begin
        -- dedoublonnage à faire ?
        Insert Into ACCOUNTS_has_MEMBERS (
            ACCOUNTS_ID_ACCOUNTS, MEMBERS_ID_MEMBERS, ACCOUNTS_has_MEMBERS_STATUS
            , ENTT_ACCNTS_ATTCHD_MBRS_PRFLES_ID_ENTT_ACCNTS_ATTCHD_MBRS_PRFLES
            , ACCOUNTS_has_MEMBERS_REF
        )
        -- explain
        Select
            src.ACCOUNTS_ID_ACCOUNTS, src.MEMBERS_ID_MEMBERS, 0
            , src.ENT_AC_ATTC_MBRS_PRFLS_ID_ENT_AC_ATTC_MBRS_PRFLS
            , src.TMP_ACCOUNTS_has_MEMBERS_REF
        From TMP_ACCOUNTS_has_MEMBERS src
        Where 
        -- les null ont été remplacés par des 0 afin que l'index soit utilisé dans less requêtes
--             src.ENT_AC_ATTC_MBRS_PRFLS_ID_ENT_AC_ATTC_MBRS_PRFLS is not null
--             And src.ACCOUNTS_ID_ACCOUNTS Is Not Null
--             src.ENT_AC_ATTC_MBRS_PRFLS_ID_ENT_AC_ATTC_MBRS_PRFLS <> 0
            src.ENT_AC_ATTC_MBRS_PRFLS_ID_ENT_AC_ATTC_MBRS_PRFLS Between 1 And 1000000
--             And src.ACCOUNTS_ID_ACCOUNTS <> 0
            And src.ACCOUNTS_ID_ACCOUNTS Between 1 And 4000000
        ;

    End;


    SET FOREIGN_KEY_CHECKS=@bamic_OLD_UNIQUE_CHECKS;
    SET UNIQUE_CHECKS=@bamic_OLD_FOREIGN_KEY_CHECKS;



End //
Delimiter ;