return: text
lang: plpgsql
src: |
  DECLARE result TEXT;
  BEGIN
  	EXECUTE 
  		'UPDATE pmsi.t_ghm_c
  		   SET 
  				code = v_ghm_5.ghm_code,
  				texte = v_ghm_5.ghm_texte,
  				ghm_prefixe = v_ghm_5.ghm_prefixe,
  				ghm_suffixe = v_ghm_5.ghm_suffixe,
  				cas_id = v_ghm_5.cas_id,
  				cas_code = v_ghm_5.cas_code,
  				cas_texte = v_ghm_5.cas_texte,
  				mco = v_ghm_5.mco,
  				mco_texte = v_ghm_5.mco_texte,
  				severite_ghm_code = v_ghm_5.severite_ghm_code,
  				severite_ghm_texte = v_ghm_5.severite_ghm_texte,
  				cmd_id = v_ghm_5.cmd_id,
  				cmd_code = v_ghm_5.cmd_code,
  				cmd_texte = v_ghm_5.cmd_texte,
  				cmd_24 = v_ghm_5.cmd_24,
  				cmd_28 = v_ghm_5.cmd_28,
  				cmd_n24n28 = v_ghm_5.cmd_n24n28,
  				groupe_activite_id = v_ghm_5.groupe_activite_id,
  				groupe_activite_code = v_ghm_5.groupe_activite_code,
  				groupe_activite_texte = v_ghm_5.groupe_activite_texte,
  				pole_oap_id = v_ghm_5.pole_oap_id,
  				pole_oap_code = v_ghm_5.pole_oap_code,
  				pole_oap_texte = v_ghm_5.pole_oap_texte,
  				ligne_oap_id = v_ghm_5.ligne_oap_id,
  				ligne_oap_code = v_ghm_5.ligne_oap_code,
  				ligne_oap_texte = v_ghm_5.ligne_oap_texte,
  				ht_fg11 = v_ghm_5.ht_fg11,
  				ghm5_id = v_ghm_5.ghm5_id,
  				ghm5_code = v_ghm_5.ghm5_code,
  				ghm5_texte = v_ghm_5.ghm5_texte
  		FROM pmsi.v_ghm_5
  		 WHERE t_ghm_c.oid = v_ghm_5.oid
  		AND (
  			t_ghm_c.code IS NULL OR t_ghm_c.texte  IS NULL OR 
  			t_ghm_c.ghm_prefixe IS NULL OR t_ghm_c.ghm_suffixe IS NULL OR 
  			t_ghm_c.mco IS NULL OR t_ghm_c.mco_texte IS NULL OR
  			t_ghm_c.cas_id IS NULL OR t_ghm_c.cas_code IS NULL OR t_ghm_c.cas_texte IS NULL OR
  			t_ghm_c.severite_ghm_code IS NULL OR t_ghm_c.severite_ghm_texte IS NULL OR 
  			t_ghm_c.cmd_id IS NULL OR t_ghm_c.cmd_code IS NULL OR t_ghm_c.cmd_texte IS NULL OR t_ghm_c.cmd_24 IS NULL OR t_ghm_c.cmd_28 IS NULL OR t_ghm_c.cmd_n24n28 IS NULL OR 
  			t_ghm_c.groupe_activite_id IS NULL OR t_ghm_c.groupe_activite_code IS NULL OR t_ghm_c.groupe_activite_texte IS NULL OR 
  			t_ghm_c.pole_oap_id IS NULL OR t_ghm_c.pole_oap_code IS NULL OR t_ghm_c.pole_oap_texte IS NULL OR 
  			t_ghm_c.ligne_oap_id IS NULL OR t_ghm_c.ligne_oap_code IS NULL OR t_ghm_c.ligne_oap_texte IS NULL OR t_ghm_c.ht_fg11 IS NULL OR 
  			t_ghm_c.ghm5_id IS NULL OR t_ghm_c.ghm5_code IS NULL OR t_ghm_c.ghm5_texte IS NULL OR
  			t_ghm_c.code <> v_ghm_5.ghm_code OR t_ghm_c.texte <> v_ghm_5.ghm_texte OR 
  			t_ghm_c.ghm_prefixe <> v_ghm_5.ghm_prefixe OR t_ghm_c.ghm_suffixe <> v_ghm_5.ghm_suffixe OR 
  			t_ghm_c.mco <> v_ghm_5.mco OR t_ghm_c.mco_texte <> v_ghm_5.mco_texte OR 
  			t_ghm_c.cas_id <> v_ghm_5.cas_id OR t_ghm_c.cas_code <> v_ghm_5.cas_code OR t_ghm_c.cas_texte <> v_ghm_5.cas_texte OR 
  			t_ghm_c.severite_ghm_code <> v_ghm_5.severite_ghm_code OR t_ghm_c.severite_ghm_texte <> v_ghm_5.severite_ghm_texte OR 
  			t_ghm_c.cmd_id <> v_ghm_5.cmd_id OR t_ghm_c.cmd_code <> v_ghm_5.cmd_code OR t_ghm_c.cmd_texte <> v_ghm_5.cmd_texte OR t_ghm_c.cmd_24 <> v_ghm_5.cmd_24 OR t_ghm_c.cmd_28 <> v_ghm_5.cmd_28 OR t_ghm_c.cmd_n24n28 <> v_ghm_5.cmd_n24n28 OR 
  			t_ghm_c.groupe_activite_id <> v_ghm_5.groupe_activite_id OR t_ghm_c.groupe_activite_code <> v_ghm_5.groupe_activite_code OR t_ghm_c.groupe_activite_texte <> v_ghm_5.groupe_activite_texte OR 
  			t_ghm_c.pole_oap_id <> v_ghm_5.pole_oap_id OR t_ghm_c.pole_oap_code <> v_ghm_5.pole_oap_code OR t_ghm_c.pole_oap_texte <> v_ghm_5.pole_oap_texte OR 
  			t_ghm_c.ligne_oap_id <> v_ghm_5.ligne_oap_id OR t_ghm_c.ligne_oap_code <> v_ghm_5.ligne_oap_code OR t_ghm_c.ligne_oap_texte <> v_ghm_5.ligne_oap_texte OR t_ghm_c.ht_fg11 <> v_ghm_5.ht_fg11 OR 
  			t_ghm_c.ghm5_id <> v_ghm_5.ghm5_id OR t_ghm_c.ghm5_code <> v_ghm_5.ghm5_code OR t_ghm_c.ghm5_texte <> v_ghm_5.ghm5_texte
  		)'
  		;

  	EXECUTE 'INSERT INTO pmsi.t_ghm_c( 
  			oid, code, texte, ghm_prefixe, ghm_suffixe, cas_id, cas_code, cas_texte, mco, mco_texte, severite_ghm_code,  
  			severite_ghm_texte, cmd_id, cmd_code, cmd_texte, ghm5_id, ghm5_code, ghm5_texte, cmd_24, cmd_28,  
  			cmd_n24n28, groupe_activite_id, groupe_activite_code, groupe_activite_texte,  
  			pole_oap_id, pole_oap_code, pole_oap_texte, ligne_oap_id, ligne_oap_code,  
  			ligne_oap_texte) 
  			SELECT oid, ghm_code, ghm_texte, ghm_prefixe, ghm_suffixe, cas_id, cas_code, cas_texte, mco, mco_texte,  
  			severite_ghm_code, severite_ghm_texte, cmd_id, cmd_code, cmd_texte, ghm5_id, ghm5_code, ghm5_texte,  
  			cmd_24, cmd_28, cmd_n24n28, groupe_activite_id, groupe_activite_code,  
  			groupe_activite_texte, pole_oap_id, pole_oap_code, pole_oap_texte,  
  			ligne_oap_id, ligne_oap_code, ligne_oap_texte 
  			FROM pmsi.v_ghm_5
  			WHERE NOT EXISTS (SELECT 1 FROM pmsi.t_ghm_c WHERE v_ghm_5.oid = t_ghm_c.oid)'
    ;

  	EXECUTE 'UPDATE pmsi.t_ghm_c SET dms_1 = t_ghm.dms_1, dms_2 = t_ghm.dms_2, dms_3 = t_ghm.dms_3, dms_hospidiag = t_ghm.dms_hospidiag
  		FROM pmsi.t_ghm
  		WHERE t_ghm.oid = t_ghm_c.oid
  		AND (t_ghm_c.dms_1 IS DISTINCT FROM t_ghm.dms_1
  			OR t_ghm_c.dms_2 IS DISTINCT FROM t_ghm.dms_2
  			OR t_ghm_c.dms_3 IS DISTINCT FROM t_ghm.dms_3
  			OR t_ghm_c.dms_hospidiag IS DISTINCT FROM t_ghm.dms_hospidiag
  		)';

  	-- Compléter les ghm nationaux pour les vieux GHM et GHM 'bidon'
  	
  	INSERT INTO pmsi.t_ghm_indicateurs_nationaux (ghm_id) 
  	SELECT t_ghm.oid
  	FROM pmsi.t_ghm
  	WHERE oid not in (select ghm_id from pmsi.t_ghm_indicateurs_nationaux)
  	;
  	RETURN 'OK';
  END;