return: text
lang: plpgsql
src: |
  DECLARE	
  	
  	
  	
  	result TEXT;
  BEGIN
  	-- Réplication des médecins dans Base
  	IF EXISTS (
  		SELECT * 
  		FROM pmsi.t_medecins 
  		WHERE oid <> 0 AND COALESCE(medecin_id,0) = 0 AND
  			t_medecins.nom <> '' AND
  			oid IN (SELECT oid FROM pmsi.p_oids WHERE code_table LIKE ('medecin%'))
  		)
  		THEN
  		BEGIN
  			PERFORM base.cti_initialize_medecins();
  			EXCEPTION 
  				WHEN others THEN RAISE NOTICE 'Erreur %' , 'base.cti_initialize_medecins'; 
  		END;
  		
  	END IF;
  	
  	-- Maintenance des spécialités absentes
  	UPDATE pmsi.t_medecins SET specialite_id = 0 WHERE specialite_id is null;
  	UPDATE pmsi.t_medecins SET specialite_interne_id = 0 WHERE specialite_interne_id is null;
  	
  	-- Mise à jour table
  
  	DELETE FROM pmsi.t_medecins_c
          WHERE pmsi.t_medecins_c.oid not in (select oid from pmsi.t_medecins);
  	
  	UPDATE pmsi.t_medecins_c
  	SET 
  		code = v_medecins_7.code, 
  		nom = v_medecins_7.nom, 
  		prenom = v_medecins_7.prenom, 
  		nom_prenom = v_medecins_7.nom_prenom, 
  		no_adeli = v_medecins_7.no_adeli, 
  		specialite_id = v_medecins_7.specialite_id, 
  		specialite_medecin_code = v_medecins_7.specialite_medecin_code, 
  		specialite_medecin_texte = v_medecins_7.specialite_medecin_texte, 
  		specialite_interne_id = v_medecins_7.specialite_interne_id, 
  		specialite_interne_medecin_code = v_medecins_7.specialite_interne_medecin_code, 
  		specialite_interne_medecin_texte = v_medecins_7.specialite_interne_medecin_texte, 
  		medecin_reference_id = v_medecins_7.medecin_reference_id, 
  		medecin_reference_code = v_medecins_7.medecin_reference_code, 
  		medecin_reference_nom = v_medecins_7.medecin_reference_nom, 
  		medecin_reference_prenom = v_medecins_7.medecin_reference_prenom, 
  		medecin_reference_nom_prenom = v_medecins_7.medecin_reference_nom_prenom, 
  		medecin_reference_no_adeli = v_medecins_7.medecin_reference_no_adeli, 
  		specialite_medecin_reference_id = v_medecins_7.specialite_medecin_reference_id, 
  		specialite_medecin_reference_code = v_medecins_7.specialite_medecin_reference_code, 
  		specialite_medecin_reference_texte = v_medecins_7.specialite_medecin_reference_texte, 
  		specialite_interne_medecin_reference_id = v_medecins_7.specialite_interne_medecin_reference_id, 
  		specialite_interne_medecin_reference_code = v_medecins_7.specialite_interne_medecin_reference_code, 
  		specialite_interne_medecin_reference_texte = v_medecins_7.specialite_interne_medecin_reference_texte
  	FROM pmsi.v_medecins_7
  	WHERE t_medecins_c.oid = v_medecins_7.oid AND
  		(
  		t_medecins_c.code IS DISTINCT FROM v_medecins_7.code OR 
  		t_medecins_c.nom IS DISTINCT FROM v_medecins_7.nom OR 
  		t_medecins_c.prenom IS DISTINCT FROM v_medecins_7.prenom OR 
  		t_medecins_c.nom_prenom IS DISTINCT FROM v_medecins_7.nom_prenom OR 
  		t_medecins_c.no_adeli IS DISTINCT FROM v_medecins_7.no_adeli OR 
  		t_medecins_c.specialite_id IS DISTINCT FROM v_medecins_7.specialite_id OR 
  		t_medecins_c.specialite_medecin_code IS DISTINCT FROM v_medecins_7.specialite_medecin_code OR 
  		t_medecins_c.specialite_medecin_texte IS DISTINCT FROM v_medecins_7.specialite_medecin_texte OR 
  		t_medecins_c.specialite_interne_id IS DISTINCT FROM v_medecins_7.specialite_interne_id OR 
  		t_medecins_c.specialite_interne_medecin_code IS DISTINCT FROM v_medecins_7.specialite_interne_medecin_code OR 
  		t_medecins_c.specialite_interne_medecin_texte IS DISTINCT FROM v_medecins_7.specialite_interne_medecin_texte OR 
  		t_medecins_c.medecin_reference_id IS DISTINCT FROM v_medecins_7.medecin_reference_id OR 
  		t_medecins_c.medecin_reference_code IS DISTINCT FROM v_medecins_7.medecin_reference_code OR 
  		t_medecins_c.medecin_reference_nom IS DISTINCT FROM v_medecins_7.medecin_reference_nom OR 
  		t_medecins_c.medecin_reference_prenom IS DISTINCT FROM v_medecins_7.medecin_reference_prenom OR 
  		t_medecins_c.medecin_reference_nom_prenom IS DISTINCT FROM v_medecins_7.medecin_reference_nom_prenom OR 
  		t_medecins_c.medecin_reference_no_adeli IS DISTINCT FROM v_medecins_7.medecin_reference_no_adeli OR 
  		t_medecins_c.specialite_medecin_reference_id IS DISTINCT FROM v_medecins_7.specialite_medecin_reference_id OR 
  		t_medecins_c.specialite_medecin_reference_code IS DISTINCT FROM v_medecins_7.specialite_medecin_reference_code OR 
  		t_medecins_c.specialite_medecin_reference_texte IS DISTINCT FROM v_medecins_7.specialite_medecin_reference_texte OR 
  		t_medecins_c.specialite_interne_medecin_reference_id IS DISTINCT FROM v_medecins_7.specialite_interne_medecin_reference_id OR 
  		t_medecins_c.specialite_interne_medecin_reference_code IS DISTINCT FROM v_medecins_7.specialite_interne_medecin_reference_code OR 
  		t_medecins_c.specialite_interne_medecin_reference_texte IS DISTINCT FROM v_medecins_7.specialite_interne_medecin_reference_texte
  	);
  	INSERT INTO pmsi.t_medecins_c( 
  		oid,
  		code,
  		nom,
  		prenom,
  		nom_prenom,
  		no_adeli,
  		specialite_id,
  		specialite_medecin_code,
  		specialite_medecin_texte,
  		specialite_interne_id,
  		specialite_interne_medecin_code,
  		specialite_interne_medecin_texte,
  		medecin_reference_id,
  		medecin_reference_code,
  		medecin_reference_nom,
  		medecin_reference_prenom,
  		medecin_reference_nom_prenom,
  		medecin_reference_no_adeli,
  		specialite_medecin_reference_id,
  		specialite_medecin_reference_code,
  		specialite_medecin_reference_texte,
  		specialite_interne_medecin_reference_id,
  		specialite_interne_medecin_reference_code,
  		specialite_interne_medecin_reference_texte) 
  	SELECT 
  		oid,
  		code,
  		nom,
  		prenom,
  		nom_prenom,
  		no_adeli,
  		specialite_id,
  		specialite_medecin_code,
  		specialite_medecin_texte,
  		specialite_interne_id,
  		specialite_interne_medecin_code,
  		specialite_interne_medecin_texte,
  		medecin_reference_id,
  		medecin_reference_code,
  		medecin_reference_nom,
  		medecin_reference_prenom,
  		medecin_reference_nom_prenom,
  		medecin_reference_no_adeli,
  		specialite_medecin_reference_id,
  		specialite_medecin_reference_code,
  		specialite_medecin_reference_texte,
  		specialite_interne_medecin_reference_id,
  		specialite_interne_medecin_reference_code,
  		specialite_interne_medecin_reference_texte 
  	FROM pmsi.v_medecins_7 
  	WHERE oid NOT IN (SELECT oid FROM pmsi.t_medecins_c);
  	PERFORM base.cti_update_classes('pmsi', 't_medecins_c', 'MEDECIN','');
  		
  	RETURN 'OK';
  END;