Delimiter //
/**
    In batchnum Int : num du lot
*/

Drop Procedure if exists LOADER_REFDATALABEL_REF_DATA_LNG_INSERT//

Create Procedure LOADER_REFDATALABEL_REF_DATA_LNG_INSERT (
    In batchnum int
    , In lastidRefData int
)
Begin
	Set @lastidRefData = lastidRefData;
	-- insertion des libelle
	--  -> des code existants (non créés par LOADER_REFDATALABEL_REF_DATA_INSERT) mais n'ayant pas de libelle dans la langue
    --  -> des codes inxistant (créés par LOADER_REFDATALABEL_REF_DATA_INSERT)
    Insert Into ENTITIES_REF_DATAS_LNG (
        ENTT_RF_DATAS_ID_ENT_RF_DATAS
        , DF_LANGUAGES_ID_DF_LANG
		, ENTITIES_REF_DATAS_LNG_LABEL
    )
    Select Distinct
			Coalesce(ARD.ID_ENTITIES_REF_DATAS, ARD.NEW_ID_ENTITIES_REF_DATAS + @lastidRefData) ID_ENTITIES_REF_DATAS
            , ARD.DF_LANGUAGES_ID_DF_LANGUAGES, ARD.ENTITIES_REF_DATAS_LNG_LABEL
    From LOADER_STATUS S
        Inner Join
            LOADER_REFDATALABEL ARD
            On ARD.LOADER_STATUS_ID_LOADER_STATUS = S.ID_LOADER_STATUS
    Where 
        S.LOADER_STATUS_ERRCOUNT = 0
        And ARD.ID_ENTITIES_REF_DATAS_LNG Is Null
--         And ARD.NEW_ID_ENTITIES_REF_DATAS_LNG Is Not Null
        AND S.LOADER_STATUS_BATCH_NUM = batchnum
-- 
    ;
End //

Delimiter ;
