Delimiter //
/**
*/

Drop Procedure if exists LOADER_EDMINDEX_DOCUMENTS_UPDATE//

Create Procedure LOADER_EDMINDEX_DOCUMENTS_UPDATE (
    In batchnum int
)
Begin
    Update DOCUMENTS trgt  
        Inner Join LOADER_EDMINDEX src  
            On trgt.ID_DOCUMENTS = src.ID_DOCUMENTS
        Inner Join LOADER_STATUS lst  
            On src.LOADER_STATUS_ID_LOADER_STATUS = lst.ID_LOADER_STATUS  

    Set  
		trgt.DOCUMENTS_SIZE = Coalesce(src.DOCUMENTS_SIZE, trgt.DOCUMENTS_SIZE)
		, trgt.DOCUMENTS_DATA = Coalesce(src.DOCUMENTS_DATA, trgt.DOCUMENTS_DATA)
		, trgt.DOCUMENTS_DATE = Coalesce(src.DOCUMENTS_DATE, trgt.DOCUMENTS_DATE, Curdate())
		, trgt.DF_MIME_TYPES_ID_DF_MIME_TYPES = Coalesce(src.DF_MIME_TYPES_ID_DF_MIME_TYPES, trgt.DF_MIME_TYPES_ID_DF_MIME_TYPES)
		, trgt.DOCUMENTS_NET_TRANSFERT_ADAPTER = Coalesce(NullIf(src.DOCUMENTS_NET_TRANSFERT_ADAPTER, '') , trgt.DOCUMENTS_NET_TRANSFERT_ADAPTER)

    Where 1 = 1  
        And lst.LOADER_STATUS_ERRCOUNT = 0  
        And lst.LOADER_STATUS_BATCH_NUM =  batchnum
    ;
End //

Delimiter ;