Delimiter $$
/**
 * Author:  armen
 * Created: 1 juil. 2020
 */
Drop Procedure if Exists BUSINESS_COMPUTE_PUBLICVIEWS_ENTRIES_REFS_LABEL $$
Create Procedure BUSINESS_COMPUTE_PUBLICVIEWS_ENTRIES_REFS_LABEL ()
Begin
        Truncate Table TMP_V_PUBLIC_ENTRIES_REFS_LABEL_VIEW;
        Insert Into TMP_V_PUBLIC_ENTRIES_REFS_LABEL_VIEW (
            ENTRIES_ID_ENTRIES, ENTITIES_REF_DATAS_ID_ENTITIES_REF_DATAS
            , ENTITIES_REF_DATAS_CODE, ENTITIES_REF_ID_ENTITIES_REF
            , ENTITIES_REF_SEQUENCE, ENT_REF_TYPE_ID_ENT_REF_TYPES
            , ENTITIES_REF_TYPES_CODE, ENTITIES_REF_DATAS_LNG_LABEL
            , FULL_ENTRIES_REFS_SEQUENCE
        )
        Select 
            ent.ID_ENTRIES, vberd.ENTITIES_REF_DATAS_ID_ENTITIES_REF_DATAS
            , vberd.ENTITIES_REF_DATAS_CODE, vberd.ENTITIES_REF_ID_ENTITIES_REF
            , vberd.ENTITIES_REF_SEQUENCE, vberd.ENT_REF_TYPE_ID_ENT_REF_TYPES
            , vberd.ENTITIES_REF_TYPES_CODE, erdl.ENTITIES_REF_DATAS_LNG_LABEL
            , Case vberd.ENTITIES_REF_TYPES_CODE
--                     When 'ACCOUNTS' Then 100
--                     When 'ACCRED_ACCOUNTS' Then 200
                    When 'ENTRIES' Then 300
                    When 'ACCRED_ENTRIES' Then 400
                    Else 300

                End 
                + vberd.ENTITIES_REF_SEQUENCE FULL_ACCOUNTS_REFS_SEQUENCE

        From
            ENTRIES ent
            Inner Join V_PUBLIC_ENTRIES_REFS_DATA vberd
                On ent.ID_ENTRIES = vberd.ENTRIES_ID_ENTRIES
            Inner Join ENTITIES_REF_DATAS_LNG erdl
                On vberd.ENTITIES_REF_DATAS_ID_ENTITIES_REF_DATAS = erdl.ENTT_RF_DATAS_ID_ENT_RF_DATAS
                And erdl.DF_LANGUAGES_ID_DF_LANG = 1
        ;
End $$
Delimiter ;