-- 1/3 : Création de la table temporaire
DROP TABLE IF EXISTS w_dbsetup
;

CREATE TEMP TABLE w_dbsetup AS
    SELECT 
    	oid,
    	name,
    	texte,
    	select_cmd,
    	code  
    FROM rh.t_listes_tables
    ORDER BY code
     LIMIT 0
;

-- 2/3 : Peuplement de la table temporaire
INSERT INTO w_dbsetup(
    oid,
    name,
    texte,
    select_cmd,
    code
)
VALUES
(1, 't_ages', 'Age', 'SELECT t_ages.oid, t_ages.code, t_ages.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM rh.t_ages LEFT JOIN rh.p_oids ON (t_ages.oid = p_oids.oid AND code_table = ''ages'')
WHERE t_ages.oid > 0 
ORDER BY t_ages.code', 'AGE'),
(1, 't_section_analytique', 'Section analytique NG', 'SELECT t_section_analytique.oid,
       t_section_analytique.code,
       t_section_analytique.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_section_analytique 
  LEFT JOIN rh.p_oids ON (t_section_analytique.oid = p_oids.oid AND code_table = ''section_analytique'')
WHERE t_section_analytique.oid > 0
ORDER BY t_section_analytique.code', 'ANA'),
(1, 't_anciennetes_annee', 'Ancienneté', 'SELECT t_anciennetes_annee.oid, t_anciennetes_annee.code, t_anciennetes_annee.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM rh.t_anciennetes_annee LEFT JOIN rh.p_oids ON (t_anciennetes_annee.oid = p_oids.oid AND code_table = ''anciennete_annee'')
ORDER BY t_anciennetes_annee.code', 'ANC'),
(1, 't_accident_circonstance', '[DICT.RH.CIRCONSTANCE] accident du travail', 'SELECT t_accident_circonstance.oid,
       t_accident_circonstance.code,
       t_accident_circonstance.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_accident_circonstance 
  LEFT JOIN rh.p_oids ON (t_accident_circonstance.oid = p_oids.oid AND code_table = ''accident_circonstance'')
WHERE t_accident_circonstance.oid > 0
ORDER BY t_accident_circonstance.code', 'ATC'),
(1, 't_accident_lieu', '[DICT.RH.LIEU] accident du travail', 'SELECT t_accident_lieu.oid,
       t_accident_lieu.code,
       t_accident_lieu.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_accident_lieu 
  LEFT JOIN rh.p_oids ON (t_accident_lieu.oid = p_oids.oid AND code_table = ''accident_lieu'')
WHERE t_accident_lieu.oid > 0
ORDER BY t_accident_lieu.code', 'ATL'),
(1, 't_accident_nature', 'Nature accident du travail', 'SELECT t_accident_nature.oid,
       t_accident_nature.code,
       t_accident_nature.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_accident_nature 
  LEFT JOIN rh.p_oids ON (t_accident_nature.oid = p_oids.oid AND code_table = ''accident_nature'')
WHERE t_accident_nature.oid > 0
ORDER BY t_accident_nature.code', 'ATN'),
(1, 't_accident_siege', 'Siège accident du travail', 'SELECT t_accident_siege.oid,
       t_accident_siege.code,
       t_accident_siege.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_accident_siege 
  LEFT JOIN rh.p_oids ON (t_accident_siege.oid = p_oids.oid AND code_table = ''accident_siege'')
WHERE t_accident_siege.oid > 0
ORDER BY t_accident_siege.code', 'ATS'),
(1, 't_categorie_conge', 'Catégorie de congé', 'SELECT t_categorie_conge.oid,
       t_categorie_conge.code,
       t_categorie_conge.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_categorie_conge 
  LEFT JOIN rh.p_oids ON (t_categorie_conge.oid = p_oids.oid AND code_table = ''categorie_conge'')
WHERE t_categorie_conge.oid > 0
ORDER BY t_categorie_conge.code', 'CAC'),
(1, 't_cadre_emploi', 'Cadre d''emploi', 'SELECT t_cadre_emploi.oid,
       t_cadre_emploi.code,
       t_cadre_emploi.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_cadre_emploi 
  LEFT JOIN rh.p_oids ON (t_cadre_emploi.oid = p_oids.oid AND code_table = ''cadre_emploi'')
WHERE t_cadre_emploi.oid > 0
ORDER BY t_cadre_emploi.code', 'CAE'),
(1, 't_categorie_statutaire', 'Catégorie statutaire', 'SELECT t_categorie_statutaire.oid,
       t_categorie_statutaire.code,
       t_categorie_statutaire.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_categorie_statutaire 
  LEFT JOIN rh.p_oids ON (t_categorie_statutaire.oid = p_oids.oid AND code_table = ''categorie_statutaire'')
WHERE t_categorie_statutaire.oid > 0
ORDER BY t_categorie_statutaire.code', 'CAS'),
(1, 't_codes_emploi', '[DICT.RH.CODE_EMPLOI]', 'SELECT t_codes_emploi.oid, t_codes_emploi.code, t_codes_emploi.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM rh.t_codes_emploi LEFT JOIN rh.p_oids ON (t_codes_emploi.oid = p_oids.oid AND code_table = ''codes_emploi'')
WHERE t_codes_emploi.oid > 0 
ORDER BY t_codes_emploi.code', 'CEM'),
(1, 't_compte_imputation', 'Compte d''imputation', 'SELECT t_compte_imputation.oid,
       t_compte_imputation.code,
       t_compte_imputation.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_compte_imputation 
  LEFT JOIN rh.p_oids ON (t_compte_imputation.oid = p_oids.oid AND code_table = ''compte_imputation'')
WHERE t_compte_imputation.oid > 0
ORDER BY t_compte_imputation.code', 'COI'),
(1, 't_code_cotisation', 'Code cotisation', 'SELECT t_code_cotisation.oid,
       t_code_cotisation.code,
       t_code_cotisation.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_code_cotisation 
  LEFT JOIN rh.p_oids ON (t_code_cotisation.oid = p_oids.oid AND code_table = ''code_cotisation'')
WHERE t_code_cotisation.oid > 0
ORDER BY t_code_cotisation.code', 'COT'),
(1, 't_codes_postaux', 'Code postal', 'SELECT t_codes_postaux.oid, t_codes_postaux.code, t_codes_postaux.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM rh.t_codes_postaux LEFT JOIN rh.p_oids ON (t_codes_postaux.oid = p_oids.oid AND code_table = ''codes_postaux'')
WHERE t_codes_postaux.oid > 0 
ORDER BY t_codes_postaux.code', 'CPO'),
(1, 't_compte', 'Compte', 'SELECT t_compte.oid,
       t_compte.code,
       t_compte.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_compte
  LEFT JOIN rh.p_oids ON (t_compte.oid = p_oids.oid AND code_table = ''compte'')
WHERE t_compte.oid > 0
ORDER BY t_compte.code', 'CPT'),
(1, 't_categories_socio_professionnelles', '[DICT.RH.CATEGORIE_SOC_PRO]', 'SELECT t_categories_socio_professionnelle.oid, t_categories_socio_professionnelle.code, t_categories_socio_professionnelle.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM rh.t_categories_socio_professionnelle LEFT JOIN rh.p_oids ON (t_categories_socio_professionnelle.oid = p_oids.oid AND code_table = ''categories_socio_professionnelle'')
WHERE t_categories_socio_professionnelle.oid > 0 
ORDER BY t_categories_socio_professionnelle.code', 'CSC'),
(1, 't_etablissements', 'Etablissement', 'SELECT t_etablissements.oid,
       t_etablissements.code,
       t_etablissements.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_etablissements 
  LEFT JOIN rh.p_oids ON (t_etablissements.oid = p_oids.oid AND code_table = ''etablissements'')
WHERE t_etablissements.oid > 0
ORDER BY t_etablissements.code', 'ETS'),
(1, 't_filiere', 'Filière', 'SELECT t_filiere.oid,
       t_filiere.code,
       t_filiere.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_filiere 
  LEFT JOIN rh.p_oids ON (t_filiere.oid = p_oids.oid AND code_table = ''filiere'')
WHERE t_filiere.oid > 0
ORDER BY t_filiere.code', 'FIL'),
(1, 't_liste_formations', 'Liste des formations', 'SELECT t_liste_formations.oid,
       t_liste_formations.code,
       t_liste_formations.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_liste_formations 
  LEFT JOIN rh.p_oids ON (t_liste_visites.oid = p_oids.oid AND code_table = ''liste_formations'')
WHERE t_liste_formations.oid > 0
ORDER BY t_liste_formations.code', 'FOR'),
(1, 't_commission_paritaire', 'Groupe de commission paritaire', 'SELECT t_commission_paritaire.oid,
       t_commission_paritaire.code,
       t_commission_paritaire.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_commission_paritaire 
  LEFT JOIN rh.p_oids ON (t_commission_paritaire.oid = p_oids.oid AND code_table = ''commission_paritaire'')
WHERE t_commission_paritaire.oid > 0
ORDER BY t_commission_paritaire.code', 'GCP'),
(1, 't_groupes_cotisant', '[DICT.RH.GROUPE_COTISANT]', 'SELECT t_groupes_cotisant.oid, t_groupes_cotisant.code, t_groupes_cotisant.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM rh.t_groupes_cotisant LEFT JOIN rh.p_oids ON (t_groupes_cotisant.oid = p_oids.oid AND code_table = ''groupes_cotisant'')
WHERE t_groupes_cotisant.oid > 0 
ORDER BY t_groupes_cotisant.code', 'GCT'),
(1, 't_grilles_groupes', '[DICT.RH.GROUPE_GRILLE]', 'SELECT t_grilles_groupes.oid,
       t_grilles_groupes.code,
       t_grilles_groupes.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_grilles_groupes 
  LEFT JOIN rh.p_oids ON (t_grilles_groupes.oid = p_oids.oid AND code_table = ''grilles_groupes'')
WHERE t_grilles_groupes.oid > 0
ORDER BY t_grilles_groupes.code', 'GRG'),
(1, 't_grilles', '[DICT.RH.GRILLE]', 'SELECT t_grilles.oid,
       t_grilles.code,
       t_grilles.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_grilles 
  LEFT JOIN rh.p_oids ON (t_grilles.oid = p_oids.oid AND code_table = ''grilles'')
WHERE t_grilles.oid > 0
ORDER BY t_grilles.code', 'GRI'),
(1, 't_lettre_budgetaire', 'Lettre Budgétaire', 'SELECT t_lettre_budgetaire.oid,
       t_lettre_budgetaire.code,
       t_lettre_budgetaire.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_lettre_budgetaire 
  LEFT JOIN rh.p_oids ON (t_lettre_budgetaire.oid = p_oids.oid AND code_table = ''lettre_budgetaire'')
WHERE t_lettre_budgetaire.oid > 0
ORDER BY t_lettre_budgetaire.code', 'LEB'),
(1, 't_motifs_arret', 'Motif [DICT.RH.ARRET_TRAVAIL#2]', 'SELECT t_motifs_arret.oid,
       t_motifs_arret.code,
       t_motifs_arret.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_motifs_arret 
  LEFT JOIN rh.p_oids ON (t_motifs_arret.oid = p_oids.oid AND code_table = ''motifs_arret'')
WHERE t_motifs_arret.oid > 0
ORDER BY t_motifs_arret.code', 'MOA'),
(1, 't_motifs_debut_contrat', '[DICT.RH.MOTIF_DEBUT#1] [DICT.RH.CONTRAT#2]', 'SELECT t_motifs_debut_contrat.oid, t_motifs_debut_contrat.code, t_motifs_debut_contrat.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM rh.t_motifs_debut_contrat LEFT JOIN rh.p_oids ON (t_motifs_debut_contrat.oid = p_oids.oid AND code_table = ''motifs_debut_contrat'')
WHERE t_motifs_debut_contrat.oid > 0 
ORDER BY t_motifs_debut_contrat.code', 'MTD'),
(1, 't_motifs_fin_contrat', 'Motif fin [DICT.RH.CONTRAT#2]', 'SELECT t_motifs_fin_contrat.oid, t_motifs_fin_contrat.code, t_motifs_fin_contrat.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM rh.t_motifs_fin_contrat LEFT JOIN rh.p_oids ON (t_motifs_fin_contrat.oid = p_oids.oid AND code_table = ''motifs_fin_contrat'')
WHERE t_motifs_fin_contrat.oid > 0 
ORDER BY t_motifs_fin_contrat.code', 'MTF'),
(1, 't_motifs_visite', 'Motif visite médicale', 'SELECT t_motifs_visite.oid,
       t_motifs_visite.code,
       t_motifs_visite.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_motifs_visite 
  LEFT JOIN rh.p_oids ON (t_motifs_visite.oid = p_oids.oid AND code_table = ''motifs_visite'')
WHERE t_motifs_visite.oid > 0
ORDER BY t_motifs_visite.code', 'MVM'),
(1, 't_nationalites', 'Nationalité', 'SELECT t_nationalites.oid,
       t_nationalites.code,
       t_nationalites.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_nationalites 
  LEFT JOIN rh.p_oids ON (t_nationalites.oid = p_oids.oid AND code_table = ''nationalites'')
WHERE t_nationalites.oid > 0
ORDER BY t_nationalites.code', 'NAT'),
(1, 't_organismes_cotisation', '[DICT.RH.ORGANISME_COTISATION]', 'SELECT t_organismes_cotisation.oid, t_organismes_cotisation.code, t_organismes_cotisation.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM rh.t_organismes_cotisation LEFT JOIN rh.p_oids ON (t_organismes_cotisation.oid = p_oids.oid AND code_table = ''organismes_cotisation'')
WHERE t_organismes_cotisation.oid > 0 
ORDER BY t_organismes_cotisation.code', 'ORC'),
(1, 't_planning_type_absence', 'Type d''absences (planning)', 'SELECT t_planning_type_absence.oid,
       t_planning_type_absence.code,
       t_planning_type_absence.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_planning_type_absence 
  LEFT JOIN rh.p_oids ON (t_planning_type_absence.oid = p_oids.oid AND code_table = ''planning_type_absence'')
WHERE t_planning_type_absence.oid > 0
ORDER BY t_planning_type_absence.code', 'PAB'),
(1, 't_planning_code_horaire', 'Code Horaire (planning)', 'SELECT t_planning_code_horaire.oid,
       t_planning_code_horaire.code,
       t_planning_code_horaire.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_planning_code_horaire 
  LEFT JOIN rh.p_oids ON (t_planning_code_horaire.oid = p_oids.oid AND code_table = ''planning_code_horaire'')
WHERE t_planning_code_horaire.oid > 0
ORDER BY t_planning_code_horaire.code', 'PCH'),
(1, 't_planning_niveau', '[DICT.RH.NIVEAU#3]', 'SELECT t_planning_niveau.oid,
       t_planning_niveau.code,
       t_planning_niveau.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_planning_niveau 
  LEFT JOIN rh.p_oids ON (t_planning_niveau.oid = p_oids.oid AND code_table = ''t_planning_niveau'')
WHERE t_planning_niveau.oid > 0
ORDER BY t_planning_niveau.code', 'PNI'),
(1, 't_population', 'Population', 'SELECT t_population.oid,
       t_population.code,
       t_population.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_population 
  LEFT JOIN rh.p_oids ON (t_population.oid = p_oids.oid AND code_table = ''population'')
WHERE t_population.oid > 0
ORDER BY t_population.code', 'POP'),
(1, 't_planning_qualification', 'Qualifications (planning)', 'SELECT t_planning_qualification.oid,
       t_planning_qualification.code,
       t_planning_qualification.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_planning_qualification 
  LEFT JOIN rh.p_oids ON (t_planning_qualification.oid = p_oids.oid AND code_table = ''planning_qualification'')
WHERE t_planning_qualification.oid > 0
ORDER BY t_planning_qualification.code', 'PQU'),
(1, 't_planning_service', 'Services (planning)', 'SELECT t_planning_service.oid,
       t_planning_service.code,
       t_planning_service.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_planning_service 
  LEFT JOIN rh.p_oids ON (t_planning_service.oid = p_oids.oid AND code_table = ''planning_service'')
WHERE t_planning_service.oid > 0
ORDER BY t_planning_service.code', 'PSE'),
(1, 't_qualifications', 'Qualification', 'SELECT t_qualifications.oid, t_qualifications.code, t_qualifications.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM rh.t_qualifications LEFT JOIN rh.p_oids ON (t_qualifications.oid = p_oids.oid AND code_table = ''qualifications'')
WHERE t_qualifications.oid > 0 
ORDER BY t_qualifications.code', 'QUA'),
(1, 't_rubriques', 'Rubrique', 'SELECT t_rubriques.oid, t_rubriques.code, t_rubriques.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM rh.t_rubriques LEFT JOIN rh.p_oids ON (t_rubriques.oid = p_oids.oid AND code_table = ''rubriques'')
WHERE t_rubriques.oid > 0 
ORDER BY t_rubriques.code', 'RUP'),
(1, 't_sections_analytiques', '[DICT.RH.SECTION_ANA_CPT#1]', 'SELECT t_sections_analytiques.oid, t_sections_analytiques.code, t_sections_analytiques.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM rh.t_sections_analytiques LEFT JOIN rh.p_oids ON (t_sections_analytiques.oid = p_oids.oid AND code_table = ''sections_analytiques'')
WHERE t_sections_analytiques.oid > 0 
ORDER BY t_sections_analytiques.code', 'SAC'),
(1, 'p_salaries', '[DICT.RH.SALARIE]', 'SELECT v_salaries_1.salarie_id AS oid, v_salaries_1.salarie_code AS code, v_salaries_1.salarie_texte || CASE WHEN date_fin_contrat < ''20991231'' THEN '' ('' || date_fin_contrat || '')'' ELSE '''' END AS texte, qualification_texte || '' - ''|| service_texte as complement, ''O''::text as utilise 
FROM rh.v_salaries_1 
ORDER BY v_salaries_1.salarie_code', 'SAL'),
(1, 't_sections_analytiques_paie', '[DICT.RH.SECTION_ANA_PAIE#1]', 'SELECT t_sections_analytiques_paie.oid, t_sections_analytiques_paie.code, t_sections_analytiques_paie.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM rh.t_sections_analytiques_paie LEFT JOIN rh.p_oids ON (t_sections_analytiques_paie.oid = p_oids.oid AND code_table = ''sections_analytiques_paie'')
WHERE t_sections_analytiques_paie.oid > 0 
ORDER BY t_sections_analytiques_paie.code', 'SAP'),
(1, 't_services', '[DICT.RH.SERVICE]', 'SELECT t_services.oid, t_services.code, t_services.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM rh.t_services LEFT JOIN rh.p_oids ON (t_services.oid = p_oids.oid AND code_table = ''services'')
WHERE t_services.oid > 0 
ORDER BY t_services.code', 'SER'),
(1, 't_specialites', 'Spécialité', 'SELECT t_specialites.oid, t_specialites.code, t_specialites.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM rh.t_specialites LEFT JOIN rh.p_oids ON (t_specialites.oid = p_oids.oid AND code_table = ''specialites'')
WHERE t_specialites.oid > 0 
ORDER BY t_specialites.code', 'SPE'),
(1, 't_statuts', '[DICT.RH.STATUT]', 'SELECT t_statuts.oid,
       t_statuts.code,
       t_statuts.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_statuts 
  LEFT JOIN rh.p_oids ON (t_statuts.oid = p_oids.oid AND code_table = ''statuts'')
WHERE t_statuts.oid > 0
ORDER BY t_statuts.code', 'STA'),
(1, 't_types_contrat', '[DICT.RH.TYPE_CONTRAT]', 'SELECT t_types_contrat.oid, t_types_contrat.code, t_types_contrat.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM rh.t_types_contrat LEFT JOIN rh.p_oids ON (t_types_contrat.oid = p_oids.oid AND code_table = ''types_contrat'')
WHERE t_types_contrat.oid > 0 
ORDER BY t_types_contrat.code', 'TCN'),
(1, 't_types_horaire', 'Type horaire', 'SELECT t_types_horaire.oid, t_types_horaire.code, t_types_horaire.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM rh.t_types_horaire LEFT JOIN rh.p_oids ON (t_types_horaire.oid = p_oids.oid AND code_table = ''types_horaire'')
WHERE t_types_horaire.oid > 0 
ORDER BY t_types_horaire.code', 'THO'),
(1, 't_types_temps_travail', 'Type temps travail', 'SELECT t_types_temps_travail.oid,
       t_types_temps_travail.code,
       t_types_temps_travail.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_types_temps_travail 
  LEFT JOIN rh.p_oids ON (t_types_temps_travail.oid = p_oids.oid AND code_table = ''types_temps_travail'')
WHERE t_types_temps_travail.oid > 0
ORDER BY t_types_temps_travail.code', 'TTT'),
(1, 't_unite_fonctionnelle', 'Unité fonctionnelle', 'SELECT t_unite_fonctionnelle.oid,
       t_unite_fonctionnelle.code,
       t_unite_fonctionnelle.texte,
       ''''::text AS complement,
       CASE
         WHEN p_oids.oid IS NOT NULL THEN ''O''
         ELSE ''N''
       END AS utilise
FROM rh.t_unite_fonctionnelle 
  LEFT JOIN rh.p_oids ON (t_unite_fonctionnelle.oid = p_oids.oid AND code_table = ''unite_fonctionnelle'')
WHERE t_unite_fonctionnelle.oid > 0
ORDER BY t_unite_fonctionnelle.code', 'UNF')
;

-- 3/3 : Màj de la table iCTI
UPDATE rh.t_listes_tables SET 
	code = w_dbsetup.code,
	texte = w_dbsetup.texte, 
	select_cmd = w_dbsetup.select_cmd
FROM w_dbsetup
WHERE t_listes_tables.name = w_dbsetup.name
;

INSERT INTO rh.t_listes_tables(oid, code, name, texte, select_cmd)
	SELECT oid, code, name, texte, select_cmd FROM w_dbsetup WHERE oid = 0 AND oid NOT IN (SELECT oid FROM rh.t_listes_tables)
;

INSERT INTO rh.t_listes_tables(code, name, texte, select_cmd)
	SELECT code, name, texte, select_cmd FROM w_dbsetup WHERE oid <> 0 AND name NOT IN (SELECT name FROM rh.t_listes_tables)
;