return: text
lang: plpgsql
src: |
  DECLARE	
  	result TEXT;
  BEGIN
  	
  	
  	TRUNCATE eco.p_oids;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'articles', article_id
  	FROM eco.p_mouvements_articles
  	WHERE article_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'articles')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'articles', article_id
  	FROM eco.p_lignes_commandes
  	WHERE article_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'articles')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'categories_articles', categorie_id
  	FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
  	WHERE categorie_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'categories_articles')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'categories_articles', categorie_id
  	FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
  	WHERE categorie_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'categories_articles')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'sous_categories_articles', sous_categorie_id
  	FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
  	WHERE sous_categorie_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'sous_categories_articles')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'sous_categories_articles', sous_categorie_id
  	FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
  	WHERE sous_categorie_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'sous_categories_articles')
  	GROUP BY 1,2;
  													
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'classification_atc', classification_atc_id
  	FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
  	WHERE classification_atc_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'classification_atc')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'classification_atc', classification_atc_id
  	FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
  	WHERE classification_atc_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'classification_atc')
  	GROUP BY 1,2;
  					
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'familles_articles', famille_id
  	FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
  	WHERE famille_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'familles_articles')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'familles_articles', famille_id
  	FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
  	WHERE famille_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'familles_articles')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'sous_familles_articles', sous_famille_id
  	FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
  	WHERE sous_famille_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'sous_familles_articles')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'sous_familles_articles', sous_famille_id
  	FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
  	WHERE sous_famille_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'sous_familles_articles')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'compte', compte_id
  	FROM eco.p_mouvements_articles
  	WHERE compte_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'compte')
  	GROUP BY 1,2;
  	
  	
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'compte', compte_id
  	FROM eco.p_lignes_commandes 
  	WHERE compte_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'compte')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'types_articles', t_articles.type_id
  	FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
  	WHERE t_articles.type_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'types_articles')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'types_articles', t_articles.type_id
  	FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
  	WHERE t_articles.type_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'types_articles')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'unites_stockage_articles', t_articles.unite_stockage_id
  	FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
  	WHERE t_articles.unite_stockage_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'unites_stockage_articles')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'unites_stockage_articles', t_articles.unite_stockage_id
  	FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
  	WHERE t_articles.unite_stockage_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'unites_stockage_articles')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'gestionnaires', gestionnaire_id
  	FROM eco.p_mouvements_articles
  	WHERE gestionnaire_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'gestionnaires')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'gestionnaires', gestionnaire_id
  	FROM eco.p_commandes
  	WHERE gestionnaire_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'gestionnaires')
  	GROUP BY 1,2;
  	
  	
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'unites_fonctionnelles', unite_fonctionnelle_id
  	FROM eco.p_mouvements_articles
  	WHERE unite_fonctionnelle_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'unites_fonctionnelles')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'unites_fonctionnelles', unite_fonctionnelle_id
  	FROM eco.p_commandes
  	WHERE unite_fonctionnelle_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'unites_fonctionnelles')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'fournisseurs', fournisseur_id
  	FROM eco.p_mouvements_articles
  	WHERE fournisseur_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'fournisseurs')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'fournisseurs', fournisseur_id
  	FROM eco.p_commandes
  	WHERE fournisseur_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'fournisseurs')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'types_fournisseurs', t_fournisseurs.type_id
  	FROM eco.p_mouvements_articles JOIN eco.t_fournisseurs ON fournisseur_id = t_fournisseurs.oid
  	WHERE t_fournisseurs.type_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'types_fournisseurs')
  	GROUP BY 1,2;
  	
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'types_fournisseurs', t_fournisseurs.type_id
  	FROM eco.p_commandes JOIN eco.t_fournisseurs ON fournisseur_id = t_fournisseurs.oid
  	WHERE t_fournisseurs.type_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'types_fournisseurs')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'lieux_mouvements', lieu_id
  	FROM eco.p_mouvements_articles
  	WHERE lieu_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'lieux_mouvements')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'lieux_commandes', lieu_livraison_id
  	FROM eco.p_commandes
  	WHERE lieu_livraison_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'lieux_commandes')
  	GROUP BY 1,2;
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'types_mouvements', type_mouvement_id
  	FROM eco.p_mouvements_articles
  	WHERE type_mouvement_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'types_mouvements')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'ucd', ucd_id
  	FROM eco.p_mouvements_articles
  	WHERE ucd_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'ucd')
  	GROUP BY 1,2;
  	
  	INSERT INTO eco.p_oids (code_table, oid) 
  	SELECT 'lpp', lpp_id
  	FROM eco.p_mouvements_articles
  	WHERE lpp_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'lpp')
  	GROUP BY 1,2;
  	
  				
  		
  	RETURN 'OK';
  END;