Delimiter //
/**
 * Build the main structure for queries in GUARANTEED context
 * @param checkedFieldName In le nom du champ à vérifier
 * @param batchnum In numero de lot
 * @param lastStatusFieldName In
 * @param lastStatus In
 * @param isWarning : 
 *  0 : maj des champs error
 *  1 : maj des champs warning
 * @out  query Out la requête généré
 * @author Ben
 */
Drop Procedure if exists LOADER_BUILD_GUARANTEED_CHECK_BASE_QUERY//

Create Procedure LOADER_BUILD_GUARANTEED_CHECK_BASE_QUERY (
    In checkedFieldName varchar(70)
    , In batchnum int
    , In flagMsg varchar(50)
    , In lastStatus varchar(150)
    , In isWarning tinyint
    , out query text
)
Begin
    Set @flagCountField='LOADER_GUARANTEED_ERRCOUNT';
    Set @flagMsgField='LOADER_GUARANTEED_ERRMSGS';
    Set @lastStatusField=null;
    Set @lastStatus=null;

    if Coalesce(isWarning, 0) = 1 Then
        Set @flagCountField='LOADER_GUARANTEED_WARNCOUNT';
        Set @flagMsgField='LOADER_GUARANTEED_WARNMSGS';
        Set @lastStatusField='';
        Set @lastStatus=lastStatus;
    End if ;


    call LOADER_BUILD_CHECK_BASE_QUERY ('`LOADER_GUARANTEED`', checkedFieldName,
        'LOADER_GUARANTEED_BATCH_NUM', batchnum, 
        @flagCountField, @flagMsgField, flagMsg, 
        @lastStatusField, @lastStatus,
        query
    );

End //
Delimiter ;
