return: text
lang: plpgsql
src: |
  DECLARE	
  	result TEXT;
  BEGIN
  	
  	
  	-- Standard ATC
  	INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
  	SELECT code_original || '99XX99', code_original || '99XX99', texte || ' (NON PRECISE)', texte_court || ' (NON PRECISE)'
  	FROM eco.t_classification_atc
  	WHERE oid <> 0 AND
  		length(code) = 1 AND
  		code_original || '99XX99' NOT IN (SELECT code FROM eco.t_classification_atc);
  	
  	INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
  	SELECT code_original || '99', code_original || '99', texte || ' (NON PRECISE)', texte_court || ' (NON PRECISE)'
  	FROM eco.t_classification_atc
  	WHERE oid <> 0 AND
  		length(code) = 1 AND
  		code_original || '99' NOT IN (SELECT code FROM eco.t_classification_atc);
  	
  	INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
  	SELECT code_original || '99X', code_original || '99X', texte || ' (NON PRECISE)', texte_court || ' (NON PRECISE)'
  	FROM eco.t_classification_atc
  	WHERE oid <> 0 AND
  		length(code) = 1 AND
  		code_original || '99X' NOT IN (SELECT code FROM eco.t_classification_atc);
  	INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
  	SELECT code_original || '99XX', code_original || '99XX', texte || ' (NON PRECISE)', texte_court || ' (NON PRECISE)'
  	FROM eco.t_classification_atc
  	WHERE oid <> 0 AND
  		length(code) = 1 AND
  		code_original || '99XX' NOT IN (SELECT code FROM eco.t_classification_atc);
  	INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
  	SELECT code_original || 'XX99', code_original || 'XX99', texte || ' (NON PRECISE)', texte_court || ' (NON PRECISE)'
  	FROM eco.t_classification_atc
  	WHERE oid <> 0 AND
  		length(code) = 3 AND substr(code,2,1) BETWEEN '0' AND '9' AND 
  		substr(code,1,1) IN (SELECT code FROM eco.t_classification_atc) AND
  		code_original || 'XX99' NOT IN (SELECT code FROM eco.t_classification_atc);
  	INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
  	SELECT code_original || 'XX', code_original || 'XX', texte || ' (NON PRECISE)', texte_court || ' (NON PRECISE)'
  	FROM eco.t_classification_atc
  	WHERE oid <> 0 AND
  		length(code) = 3 AND substr(code,2,1) BETWEEN '0' AND '9' AND 
  		substr(code,1,1) IN (SELECT code FROM eco.t_classification_atc) AND
  		code_original || 'XX' NOT IN (SELECT code FROM eco.t_classification_atc);
  	INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
  	SELECT code_original || 'X', code_original || 'X', texte || ' (NON PRECISE)', texte_court || ' (NON PRECISE)'
  	FROM eco.t_classification_atc
  	WHERE oid <> 0 AND
  		length(code) = 3 AND substr(code,2,1) BETWEEN '0' AND '9' AND 
  		substr(code,1,1) IN (SELECT code FROM eco.t_classification_atc) AND
  		code_original || 'X' NOT IN (SELECT code FROM eco.t_classification_atc);
  	INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
  	SELECT code_original || 'X99', code_original || 'X99', texte || ' (NON PRECISE)', texte_court || ' (NON PRECISE)'
  	FROM eco.t_classification_atc
  	WHERE oid <> 0 AND
  		length(code) = 4 AND substr(code,2,1) BETWEEN '0' AND '9' AND 
  		substr(code,1,3) IN (SELECT code FROM eco.t_classification_atc) AND
  		code_original || 'X99' NOT IN (SELECT code FROM eco.t_classification_atc);
  	INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
  	SELECT code_original || 'X', code_original || 'X', texte || ' (NON PRECISE)', texte_court || ' (NON PRECISE)'
  	FROM eco.t_classification_atc
  	WHERE oid <> 0 AND
  		length(code) = 4 AND substr(code,2,1) BETWEEN '0' AND '9' AND 
  		substr(code,1,3) IN (SELECT code FROM eco.t_classification_atc) AND
  		code_original || 'X' NOT IN (SELECT code FROM eco.t_classification_atc);
  	INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
  	SELECT code_original || '99', code_original || '99', texte || ' (NON PRECISE)', texte_court || ' (NON PRECISE)'
  	FROM eco.t_classification_atc
  	WHERE oid <> 0 AND
  		length(code) = 5 AND substr(code,2,1) BETWEEN '0' AND '9' AND 
  		substr(code,1,4) IN (SELECT code FROM eco.t_classification_atc) AND
  		code_original || '99' NOT IN (SELECT code FROM eco.t_classification_atc);
  	
  	-- Spécial préfixe à deux caractères
  	
  	INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
  	SELECT code_original || '99XX99', code_original || '99XX99', texte || ' (NON PRECISE)', texte_court || ' (NON PRECISE)'
  	FROM eco.t_classification_atc
  	WHERE oid <> 0 AND
  		length(code) = 2 AND substr(code,2,1) NOT BETWEEN '0' AND '9' AND 
  		code_original || '99XX99' NOT IN (SELECT code FROM eco.t_classification_atc);
  	
  	INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
  	SELECT code_original || '99', code_original || '99', texte || ' (NON PRECISE)', texte_court || ' (NON PRECISE)'
  	FROM eco.t_classification_atc
  	WHERE oid <> 0 AND
  		length(code) = 2 AND substr(code,2,1) NOT BETWEEN '0' AND '9' AND 
  		code_original || '99' NOT IN (SELECT code FROM eco.t_classification_atc);
  	
  	INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
  	SELECT code_original || '99X', code_original || '99X', texte || ' (NON PRECISE)', texte_court || ' (NON PRECISE)'
  	FROM eco.t_classification_atc
  	WHERE oid <> 0 AND
  		length(code) = 2 AND substr(code,2,1) NOT BETWEEN '0' AND '9' AND 
  		code_original || '99X' NOT IN (SELECT code FROM eco.t_classification_atc);
  	INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
  	SELECT code_original || '99XX', code_original || '99XX', texte || ' (NON PRECISE)', texte_court || ' (NON PRECISE)'
  	FROM eco.t_classification_atc
  	WHERE oid <> 0 AND
  		length(code) = 2 AND substr(code,2,1) NOT BETWEEN '0' AND '9' AND 
  		code_original || '99XX' NOT IN (SELECT code FROM eco.t_classification_atc);
  	INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
  	SELECT code_original || 'XX99', code_original || 'XX99', texte || ' (NON PRECISE)', texte_court || ' (NON PRECISE)'
  	FROM eco.t_classification_atc
  	WHERE oid <> 0 AND
  		length(code) = 4 AND substr(code,2,1) NOT BETWEEN '0' AND '9' AND 
  		substr(code,1,2) IN (SELECT code FROM eco.t_classification_atc) AND
  		code_original || 'XX99' NOT IN (SELECT code FROM eco.t_classification_atc);
  	INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
  	SELECT code_original || 'XX', code_original || 'XX', texte || ' (NON PRECISE)', texte_court || ' (NON PRECISE)'
  	FROM eco.t_classification_atc
  	WHERE oid <> 0 AND
  		length(code) = 4 AND substr(code,2,1) NOT BETWEEN '0' AND '9' AND 
  		substr(code,1,2) IN (SELECT code FROM eco.t_classification_atc) AND
  		code_original || 'XX' NOT IN (SELECT code FROM eco.t_classification_atc);
  	INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
  	SELECT code_original || 'X', code_original || 'X', texte || ' (NON PRECISE)', texte_court || ' (NON PRECISE)'
  	FROM eco.t_classification_atc
  	WHERE oid <> 0 AND
  		length(code) = 4 AND substr(code,2,1) NOT BETWEEN '0' AND '9' AND 
  		substr(code,1,2) IN (SELECT code FROM eco.t_classification_atc) AND
  		code_original || 'X' NOT IN (SELECT code FROM eco.t_classification_atc);
  	INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
  	SELECT code_original || 'X99', code_original || 'X99', texte || ' (NON PRECISE)', texte_court || ' (NON PRECISE)'
  	FROM eco.t_classification_atc
  	WHERE oid <> 0 AND
  		length(code) = 5 AND substr(code,2,1) NOT BETWEEN '0' AND '9' AND 
  		substr(code,1,4) IN (SELECT code FROM eco.t_classification_atc) AND
  		code_original || 'X99' NOT IN (SELECT code FROM eco.t_classification_atc);
  	INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
  	SELECT code_original || 'X', code_original || 'X', texte || ' (NON PRECISE)', texte_court || ' (NON PRECISE)'
  	FROM eco.t_classification_atc
  	WHERE oid <> 0 AND
  		length(code) = 5 AND substr(code,2,1) NOT BETWEEN '0' AND '9' AND 
  		substr(code,1,4) IN (SELECT code FROM eco.t_classification_atc) AND
  		code_original || 'X' NOT IN (SELECT code FROM eco.t_classification_atc);
  	INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
  	SELECT code_original || '99', code_original || '99', texte || ' (NON PRECISE)', texte_court || ' (NON PRECISE)'
  	FROM eco.t_classification_atc
  	WHERE oid <> 0 AND
  		length(code) = 6 AND substr(code,2,1) NOT BETWEEN '0' AND '9' AND 
  		substr(code,1,5) IN (SELECT code FROM eco.t_classification_atc) AND
  		code_original || '99' NOT IN (SELECT code FROM eco.t_classification_atc);
  	-- compléments
  	UPDATE eco.t_classification_atc SET
  		texte = trim(replace(texte,'(NON PRECISE)','')),
  		texte_court = trim(replace(texte,'(NON PRECISE)',''))
  	WHERE code like 'Z%' AND texte like '%(NON PRECISE)%';
  	UPDATE eco.t_classification_atc SET
  		classification_atc_1_id = t_classification_atc_1.oid,
  		classification_atc_1_code = t_classification_atc_1.code,
  		classification_atc_1_texte = t_classification_atc_1.texte
  	FROM eco.t_classification_atc t_classification_atc_1
  	WHERE t_classification_atc.oid <> 0 AND
  		substr(t_classification_atc.code,1,1) = t_classification_atc_1.code AND
  		substr(t_classification_atc.code,2,1) BETWEEN '0' AND '9' AND
  		(
  		t_classification_atc.classification_atc_1_id IS DISTINCT FROM t_classification_atc_1.oid OR
  		t_classification_atc.classification_atc_1_code IS DISTINCT FROM t_classification_atc_1.code OR
  		t_classification_atc.classification_atc_1_texte IS DISTINCT FROM t_classification_atc_1.texte
  		);
  	UPDATE eco.t_classification_atc SET
  		classification_atc_1_id = t_classification_atc_1.oid,
  		classification_atc_1_code = t_classification_atc_1.code,
  		classification_atc_1_texte = t_classification_atc_1.texte
  	FROM eco.t_classification_atc t_classification_atc_1
  	WHERE t_classification_atc.oid <> 0 AND
  		substr(t_classification_atc.code,1,2) = t_classification_atc_1.code AND
  		substr(t_classification_atc.code,2,1) NOT BETWEEN '0' AND '9' AND
  		(
  		t_classification_atc.classification_atc_1_id IS DISTINCT FROM t_classification_atc_1.oid OR
  		t_classification_atc.classification_atc_1_code IS DISTINCT FROM t_classification_atc_1.code OR
  		t_classification_atc.classification_atc_1_texte IS DISTINCT FROM t_classification_atc_1.texte
  		);
  	UPDATE eco.t_classification_atc SET
  		classification_atc_1_id = t_classification_atc_1.oid,
  		classification_atc_1_code = t_classification_atc_1.code,
  		classification_atc_1_texte = t_classification_atc_1.texte
  	FROM eco.t_classification_atc t_classification_atc_1
  	WHERE t_classification_atc.oid <> 0 AND
  		t_classification_atc_1.code = 'X' AND COALESCE(t_classification_atc.classification_atc_1_id,0) = 0 AND
  		(
  		t_classification_atc.classification_atc_1_id IS DISTINCT FROM t_classification_atc_1.oid OR
  		t_classification_atc.classification_atc_1_code IS DISTINCT FROM t_classification_atc_1.code OR
  		t_classification_atc.classification_atc_1_texte IS DISTINCT FROM t_classification_atc_1.texte
  		);
  	UPDATE eco.t_classification_atc SET
  		classification_atc_1_code = t_classification_atc_1.code,
  		classification_atc_1_texte = t_classification_atc_1.texte
  	FROM eco.t_classification_atc t_classification_atc_1
  	WHERE t_classification_atc.classification_atc_1_id = t_classification_atc_1.oid AND
  		(
  		t_classification_atc.classification_atc_1_code IS DISTINCT FROM t_classification_atc_1.code OR
  		t_classification_atc.classification_atc_1_texte IS DISTINCT FROM t_classification_atc_1.texte
  		);
  	UPDATE eco.t_classification_atc SET
  		classification_atc_2_id = t_classification_atc_1.oid,
  		classification_atc_2_code = t_classification_atc_1.code,
  		classification_atc_2_texte = t_classification_atc_1.texte
  	FROM eco.t_classification_atc t_classification_atc_1
  	WHERE t_classification_atc.oid <> 0 AND
  		substr(t_classification_atc.code,1,3) = t_classification_atc_1.code AND
  		substr(t_classification_atc.code,2,1) BETWEEN '0' AND '9' AND
  		length(t_classification_atc.code) >= 3 AND
  		(
  		t_classification_atc.classification_atc_2_id IS DISTINCT FROM t_classification_atc_1.oid OR
  		t_classification_atc.classification_atc_2_code IS DISTINCT FROM t_classification_atc_1.code OR
  		t_classification_atc.classification_atc_2_texte IS DISTINCT FROM t_classification_atc_1.texte
  		);
  	UPDATE eco.t_classification_atc SET
  		classification_atc_2_id = t_classification_atc_1.oid,
  		classification_atc_2_code = t_classification_atc_1.code,
  		classification_atc_2_texte = t_classification_atc_1.texte
  	FROM eco.t_classification_atc t_classification_atc_1
  	WHERE t_classification_atc.oid <> 0 AND
  		substr(t_classification_atc.code,1,4) = t_classification_atc_1.code AND
  		substr(t_classification_atc.code,2,1) NOT BETWEEN '0' AND '9' AND
  		length(t_classification_atc.code) >= 4 AND
  		(
  		t_classification_atc.classification_atc_2_id IS DISTINCT FROM t_classification_atc_1.oid OR
  		t_classification_atc.classification_atc_2_code IS DISTINCT FROM t_classification_atc_1.code OR
  		t_classification_atc.classification_atc_2_texte IS DISTINCT FROM t_classification_atc_1.texte
  		);
  	UPDATE eco.t_classification_atc SET
  		classification_atc_2_id = t_classification_atc_1.oid,
  		classification_atc_2_code = t_classification_atc_1.code,
  		classification_atc_2_texte = t_classification_atc_1.texte
  	FROM eco.t_classification_atc t_classification_atc_1
  	WHERE t_classification_atc.oid <> 0 AND
  		t_classification_atc_1.code = t_classification_atc.classification_atc_1_code || '99' AND COALESCE(t_classification_atc.classification_atc_2_id,0) = 0 AND
  		(
  		t_classification_atc.classification_atc_2_id IS DISTINCT FROM t_classification_atc_1.oid OR
  		t_classification_atc.classification_atc_2_code IS DISTINCT FROM t_classification_atc_1.code OR
  		t_classification_atc.classification_atc_2_texte IS DISTINCT FROM t_classification_atc_1.texte
  		);
  	UPDATE eco.t_classification_atc SET
  		classification_atc_2_code = t_classification_atc_1.code,
  		classification_atc_2_texte = t_classification_atc_1.texte
  	FROM eco.t_classification_atc t_classification_atc_1
  	WHERE t_classification_atc.classification_atc_2_id = t_classification_atc_1.oid AND
  		(
  		t_classification_atc.classification_atc_2_code IS DISTINCT FROM t_classification_atc_1.code OR
  		t_classification_atc.classification_atc_2_texte IS DISTINCT FROM t_classification_atc_1.texte
  		);
  	UPDATE eco.t_classification_atc SET
  		classification_atc_3_id = t_classification_atc_1.oid,
  		classification_atc_3_code = t_classification_atc_1.code,
  		classification_atc_3_texte = t_classification_atc_1.texte
  	FROM eco.t_classification_atc t_classification_atc_1
  	WHERE t_classification_atc.oid <> 0 AND
  		substr(t_classification_atc.code,1,4) = t_classification_atc_1.code AND
  		substr(t_classification_atc.code,2,1) BETWEEN '0' AND '9' AND
  		length(t_classification_atc.code) >= 4 AND
  		(
  		t_classification_atc.classification_atc_3_id IS DISTINCT FROM t_classification_atc_1.oid OR
  		t_classification_atc.classification_atc_3_code IS DISTINCT FROM t_classification_atc_1.code OR
  		t_classification_atc.classification_atc_3_texte IS DISTINCT FROM t_classification_atc_1.texte
  		);
  	UPDATE eco.t_classification_atc SET
  		classification_atc_3_id = t_classification_atc_1.oid,
  		classification_atc_3_code = t_classification_atc_1.code,
  		classification_atc_3_texte = t_classification_atc_1.texte
  	FROM eco.t_classification_atc t_classification_atc_1
  	WHERE t_classification_atc.oid <> 0 AND
  		substr(t_classification_atc.code,1,5) = t_classification_atc_1.code AND
  		substr(t_classification_atc.code,2,1) NOT BETWEEN '0' AND '9' AND
  		length(t_classification_atc.code) >= 5 AND
  		(
  		t_classification_atc.classification_atc_3_id IS DISTINCT FROM t_classification_atc_1.oid OR
  		t_classification_atc.classification_atc_3_code IS DISTINCT FROM t_classification_atc_1.code OR
  		t_classification_atc.classification_atc_3_texte IS DISTINCT FROM t_classification_atc_1.texte
  		);
  	UPDATE eco.t_classification_atc SET
  		classification_atc_3_id = t_classification_atc_1.oid,
  		classification_atc_3_code = t_classification_atc_1.code,
  		classification_atc_3_texte = t_classification_atc_1.texte
  	FROM eco.t_classification_atc t_classification_atc_1
  	WHERE t_classification_atc.oid <> 0 AND
  		t_classification_atc_1.code = t_classification_atc.classification_atc_2_code || 'X' AND COALESCE(t_classification_atc.classification_atc_3_id,0) = 0 AND
  		(
  		t_classification_atc.classification_atc_3_id IS DISTINCT FROM t_classification_atc_1.oid OR
  		t_classification_atc.classification_atc_3_code IS DISTINCT FROM t_classification_atc_1.code OR
  		t_classification_atc.classification_atc_3_texte IS DISTINCT FROM t_classification_atc_1.texte
  		);
  	UPDATE eco.t_classification_atc SET
  		classification_atc_3_code = t_classification_atc_1.code,
  		classification_atc_3_texte = t_classification_atc_1.texte
  	FROM eco.t_classification_atc t_classification_atc_1
  	WHERE t_classification_atc.classification_atc_3_id = t_classification_atc_1.oid AND
  		(
  		t_classification_atc.classification_atc_3_code IS DISTINCT FROM t_classification_atc_1.code OR
  		t_classification_atc.classification_atc_3_texte IS DISTINCT FROM t_classification_atc_1.texte
  		);
  	UPDATE eco.t_classification_atc SET
  		classification_atc_4_id = t_classification_atc_1.oid,
  		classification_atc_4_code = t_classification_atc_1.code,
  		classification_atc_4_texte = t_classification_atc_1.texte
  	FROM eco.t_classification_atc t_classification_atc_1
  	WHERE t_classification_atc.oid <> 0 AND
  		substr(t_classification_atc.code,2,1) BETWEEN '0' AND '9' AND
  		substr(t_classification_atc.code,1,5) = t_classification_atc_1.code AND
  		length(t_classification_atc.code) >= 5 AND
  		(
  		t_classification_atc.classification_atc_4_id IS DISTINCT FROM t_classification_atc_1.oid OR
  		t_classification_atc.classification_atc_4_code IS DISTINCT FROM t_classification_atc_1.code OR
  		t_classification_atc.classification_atc_4_texte IS DISTINCT FROM t_classification_atc_1.texte
  		);
  	UPDATE eco.t_classification_atc SET
  		classification_atc_4_id = t_classification_atc_1.oid,
  		classification_atc_4_code = t_classification_atc_1.code,
  		classification_atc_4_texte = t_classification_atc_1.texte
  	FROM eco.t_classification_atc t_classification_atc_1
  	WHERE t_classification_atc.oid <> 0 AND
  		substr(t_classification_atc.code,2,1) NOT BETWEEN '0' AND '9' AND
  		substr(t_classification_atc.code,1,6) = t_classification_atc_1.code AND
  		length(t_classification_atc.code) >= 6 AND
  		(
  		t_classification_atc.classification_atc_4_id IS DISTINCT FROM t_classification_atc_1.oid OR
  		t_classification_atc.classification_atc_4_code IS DISTINCT FROM t_classification_atc_1.code OR
  		t_classification_atc.classification_atc_4_texte IS DISTINCT FROM t_classification_atc_1.texte
  		);
  	UPDATE eco.t_classification_atc SET
  		classification_atc_4_id = t_classification_atc_1.oid,
  		classification_atc_4_code = t_classification_atc_1.code,
  		classification_atc_4_texte = t_classification_atc_1.texte
  	FROM eco.t_classification_atc t_classification_atc_1
  	WHERE t_classification_atc.oid <> 0 AND
  		t_classification_atc_1.code = t_classification_atc.classification_atc_3_code || 'X' AND COALESCE(t_classification_atc.classification_atc_4_id,0) = 0 AND
  		(
  		t_classification_atc.classification_atc_4_id IS DISTINCT FROM t_classification_atc_1.oid OR
  		t_classification_atc.classification_atc_4_code IS DISTINCT FROM t_classification_atc_1.code OR
  		t_classification_atc.classification_atc_4_texte IS DISTINCT FROM t_classification_atc_1.texte
  		);
  	UPDATE eco.t_classification_atc SET
  		classification_atc_4_code = t_classification_atc_1.code,
  		classification_atc_4_texte = t_classification_atc_1.texte
  	FROM eco.t_classification_atc t_classification_atc_1
  	WHERE t_classification_atc.classification_atc_4_id = t_classification_atc_1.oid AND
  		(
  		t_classification_atc.classification_atc_4_code IS DISTINCT FROM t_classification_atc_1.code OR
  		t_classification_atc.classification_atc_4_texte IS DISTINCT FROM t_classification_atc_1.texte
  		);
  	UPDATE eco.t_classification_atc SET
  		classification_atc_1_id = oid,
  		classification_atc_1_code = code,
  		classification_atc_1_texte = texte_court,
  		classification_atc_2_id = oid,
  		classification_atc_2_code = code,
  		classification_atc_2_texte = texte_court,
  		classification_atc_3_id = oid,
  		classification_atc_3_code = code,
  		classification_atc_3_texte = texte_court,
  		classification_atc_4_id = oid,
  		classification_atc_4_code = code,
  		classification_atc_4_texte = texte_court
  	WHERE 
  		(
  		classification_atc_1_code = 'X' AND code NOT LIKE 'X%' OR
  		classification_atc_1_id = 0 OR 
  		classification_atc_1_id IS NULL
  		) 
  		AND
  		(classification_atc_1_id IS DISTINCT FROM oid OR
  		classification_atc_1_code IS DISTINCT FROM code OR
  		classification_atc_1_texte IS DISTINCT FROM texte_court OR
  		classification_atc_2_id IS DISTINCT FROM oid OR
  		classification_atc_2_code IS DISTINCT FROM code OR
  		classification_atc_2_texte IS DISTINCT FROM texte_court OR
  		classification_atc_3_id IS DISTINCT FROM oid OR
  		classification_atc_3_code IS DISTINCT FROM code OR
  		classification_atc_3_texte IS DISTINCT FROM texte_court OR
  		classification_atc_4_id IS DISTINCT FROM oid OR
  		classification_atc_4_code IS DISTINCT FROM code OR
  		classification_atc_4_texte IS DISTINCT FROM texte_court
  		)
  		;
  		-- Ventilation ATC/CLADIMED
  		UPDATE eco.t_classification_atc SET
  			est_atc = subview.est_atc,
  			est_cladimed = subview.est_cladimed
  		FROM 
  			(SELECT substr(t_classification_atc.code,1,1) AS code, MAX(est_atc) AS est_atc, MAX(est_cladimed) AS est_cladimed
  			FROM eco.t_classification_atc
  			WHERE oid <> 0
  			GROUP BY 1
  			ORDER BY 1
  			) subview
  		WHERE substr(t_classification_atc.code,2,6) = '99' AND
  			substr(t_classification_atc.code,1,1) = subview.code AND
  			(
  			t_classification_atc.est_atc IS DISTINCT FROM subview.est_atc OR
  			t_classification_atc.est_cladimed IS DISTINCT FROM subview.est_cladimed
  			)
  			;
  		UPDATE eco.t_classification_atc SET
  			est_atc = subview.est_atc,
  			est_cladimed = subview.est_cladimed
  		FROM 
  			(SELECT substr(t_classification_atc.code,1,3) AS code, MAX(est_atc) AS est_atc, MAX(est_cladimed) AS est_cladimed
  			FROM eco.t_classification_atc
  			WHERE oid <> 0
  			GROUP BY 1
  			ORDER BY 1
  			) subview
  		WHERE substr(t_classification_atc.code,4,4) = 'X' AND
  			substr(t_classification_atc.code,1,3) = subview.code AND
  			(
  			t_classification_atc.est_atc IS DISTINCT FROM subview.est_atc OR
  			t_classification_atc.est_cladimed IS DISTINCT FROM subview.est_cladimed
  			)
  			;
  			
  		UPDATE eco.t_classification_atc SET
  			est_atc = subview.est_atc,
  			est_cladimed = subview.est_cladimed
  		FROM 
  			(SELECT substr(t_classification_atc.code,1,4) AS code, MAX(est_atc) AS est_atc, MAX(est_cladimed) AS est_cladimed
  			FROM eco.t_classification_atc
  			WHERE oid <> 0
  			GROUP BY 1
  			ORDER BY 1
  			) subview
  		WHERE substr(t_classification_atc.code,5,3) = 'X' AND
  			substr(t_classification_atc.code,1,4) = subview.code AND
  			(
  			t_classification_atc.est_atc IS DISTINCT FROM subview.est_atc OR
  			t_classification_atc.est_cladimed IS DISTINCT FROM subview.est_cladimed
  			)
  			;
  			
  		UPDATE eco.t_classification_atc SET
  			est_atc = subview.est_atc,
  			est_cladimed = subview.est_cladimed
  		FROM 
  			(SELECT substr(t_classification_atc.code,1,5) AS code, MAX(est_atc) AS est_atc, MAX(est_cladimed) AS est_cladimed
  			FROM eco.t_classification_atc
  			WHERE oid <> 0
  			GROUP BY 1
  			ORDER BY 1
  			) subview
  		WHERE substr(t_classification_atc.code,6,2) = 'XX' AND
  			substr(t_classification_atc.code,1,5) = subview.code AND
  			(
  			t_classification_atc.est_atc IS DISTINCT FROM subview.est_atc OR
  			t_classification_atc.est_cladimed IS DISTINCT FROM subview.est_cladimed
  			)
  			;
  
  		-- Atc dans articles selon ucd
  		UPDATE eco.t_articles SET
  			classification_atc_id = t_classification_atc.oid
  		FROM base.t_ucd
  		JOIN base.t_classification_atc t_classification_atc_base ON t_ucd.classification_atc_id = t_classification_atc_base.oid
  		JOIN eco.t_classification_atc ON t_classification_atc_base.code = t_classification_atc.code
  		WHERE t_articles.ucd_id = t_ucd.oid AND
  			t_articles.ucd_id <> 0 AND
  			t_classification_atc.oid <> 0 AND
  			(
  			t_articles.classification_atc_id IS DISTINCT FROM t_classification_atc.oid
  			)
  		;
  
  		-- UPDATE eco.t_articles SET
  		-- 	classification_atc_id = 0
  		-- WHERE ucd_id = 0 AND
  		-- 	lpp_id = 0 AND
  		-- 	classification_atc_id <> 0
  		-- ;
  
  		-- Les articles sans ATC sont affectés à Z99XX99 (Verrouillé en attendant alanyse : ELA)
  		UPDATE eco.t_articles SET
  			classification_atc_id = t_classification_atc.oid
  		FROM eco.t_classification_atc
  		WHERE 1<>1 AND
  			(ucd_id <> 0 OR lpp_id <> 0) AND
  			classification_atc_id = 0 AND
  			t_classification_atc.code = 'Z99XX99' AND
  			(
  				t_articles.classification_atc_id IS DISTINCT FROM t_classification_atc.oid
  			)
  		;
  
  		UPDATE eco.t_articles SET
  			classification_atc_id = 0
  		WHERE classification_atc_id IN (SELECT oid FROM eco.t_classification_atc WHERE code LIKE 'Z99%')
  		;
  
  		
  
  		UPDATE eco.t_articles SET
  			classification_atc_code = t_classification_atc.code,
  			classification_atc_texte = t_classification_atc.texte_court,
  			classification_atc_section_id = t_classification_atc.section_id,
  			classification_atc_section_code = t_classification_atc.section_code,
  			classification_atc_section_texte = t_classification_atc.section_texte,
  			classification_atc_1_id = t_classification_atc.classification_atc_1_id,
  			classification_atc_1_code = t_classification_atc.classification_atc_1_code,
  			classification_atc_1_texte = t_classification_atc.classification_atc_1_texte,
  			classification_atc_2_id = t_classification_atc.classification_atc_2_id,
  			classification_atc_2_code = t_classification_atc.classification_atc_2_code,
  			classification_atc_2_texte = t_classification_atc.classification_atc_2_texte,
  			classification_atc_3_id = t_classification_atc.classification_atc_3_id,
  			classification_atc_3_code = t_classification_atc.classification_atc_3_code,
  			classification_atc_3_texte = t_classification_atc.classification_atc_3_texte,
  			classification_atc_4_id = t_classification_atc.classification_atc_4_id,
  			classification_atc_4_code = t_classification_atc.classification_atc_4_code,
  			classification_atc_4_texte = t_classification_atc.classification_atc_4_texte
  		FROM eco.t_classification_atc
  		WHERE t_articles.classification_atc_id = t_classification_atc.oid  AND
  			(
  				t_articles.classification_atc_code IS DISTINCT FROM t_classification_atc.code OR
  				t_articles.classification_atc_texte IS DISTINCT FROM t_classification_atc.texte_court OR
  				t_articles.classification_atc_section_id IS DISTINCT FROM t_classification_atc.section_id OR
  				t_articles.classification_atc_section_code IS DISTINCT FROM t_classification_atc.section_code OR
  				t_articles.classification_atc_section_texte IS DISTINCT FROM t_classification_atc.section_texte OR
  				t_articles.classification_atc_1_id IS DISTINCT FROM t_classification_atc.classification_atc_1_id OR
  				t_articles.classification_atc_1_code IS DISTINCT FROM t_classification_atc.classification_atc_1_code OR
  				t_articles.classification_atc_1_texte IS DISTINCT FROM t_classification_atc.classification_atc_1_texte OR
  				t_articles.classification_atc_2_id IS DISTINCT FROM t_classification_atc.classification_atc_2_id OR
  				t_articles.classification_atc_2_code IS DISTINCT FROM t_classification_atc.classification_atc_2_code OR
  				t_articles.classification_atc_2_texte IS DISTINCT FROM t_classification_atc.classification_atc_2_texte OR
  				t_articles.classification_atc_3_id IS DISTINCT FROM t_classification_atc.classification_atc_3_id OR
  				t_articles.classification_atc_3_code IS DISTINCT FROM t_classification_atc.classification_atc_3_code OR
  				t_articles.classification_atc_3_texte IS DISTINCT FROM t_classification_atc.classification_atc_3_texte OR
  				t_articles.classification_atc_4_id IS DISTINCT FROM t_classification_atc.classification_atc_4_id OR
  				t_articles.classification_atc_4_code IS DISTINCT FROM t_classification_atc.classification_atc_4_code OR
  				t_articles.classification_atc_4_texte IS DISTINCT FROM t_classification_atc.classification_atc_4_texte
  			);
  		
  
  	
  	
  		
  	RETURN 'OK';
  END;