Delimiter //
/**
 * Updates the account last managment level
 *
 * @author Benoit
 * @ticket #4268
 */ 
Drop Procedure if Exists BUSINESS_UPDATE_ACCOUNT_MNGT_LVL//
 
Create Procedure BUSINESS_UPDATE_ACCOUNT_MNGT_LVL(In idaccounts Int unsigned, In identities Int unsigned)
Begin
-- 	set @accFilter = ifnull(idaccounts, '%');
    if idaccounts Is null Then
        Set @buaml_accMinVal = 0;
        Set @buaml_accMaxVal = 4e7;
    Else
        Set @buaml_accMinVal = idaccounts;
        Set @buaml_accMaxVal = idaccounts;
    End if;

    if identities Is null Then
        Set @buaml_enttMinVal = 0;
        Set @buaml_enttMaxVal = 1000;
    Else
        Set @buaml_enttMinVal = identities;
        Set @buaml_enttMaxVal = identities;
    End if;


    Update
        ACCOUNTS ACC
        INNER JOIN FI_ANA FIA
            ON FIA.ACCOUNTS_ID_ACCOUNTS = ACC.ID_ACCOUNTS
    Set
        ACC.ACCOUNTS_ACC_MNGT_LAST_LVL = 1
    Where
        FIA.FI_ANA_OUTSTANDING = 0
        And ACC.ACCOUNTS_ACC_MNGT_LAST_LVL > 1
		And ACC.ID_ACCOUNTS between @buaml_accMinVal And @buaml_accMaxVal
		And ACC.ENTITIES_ID_ENTITIES between @buaml_enttMinVal And @buaml_enttMaxVal

    ;

End //
Delimiter ;