return: text
lang: plpgsql
parameters:
  p0:
    type: text
    name: tablename
  p1:
    type: text
    name: field_prefix
src: |
  DECLARE	
  	w_tablename TEXT;
  	w_field_prefix TEXT;
  	result TEXT;
  BEGIN
  	
  	w_tablename = tablename;
  	IF w_tablename = '' THEN
  		w_tablename = 't_articles';
  	END IF;
  	
  	w_field_prefix = field_prefix;
  	IF w_field_prefix = '' THEN
  		w_field_prefix = 'article';
  	END IF;
  	
  	EXECUTE 'UPDATE econom.p_profils
  		SET ' || w_field_prefix || '_id = ' || w_tablename || '.oid
  		FROM econom.' || w_tablename || '
  		WHERE ' || w_tablename || '.code_original = p_profils.' || w_field_prefix || '_code_original
  			AND ' || w_field_prefix || '_id IS DISTINCT FROM ' || w_tablename || '.oid;';
  	EXECUTE 'UPDATE econom.p_profils SET ' || w_field_prefix || '_id = 0 WHERE ' || w_field_prefix || '_id IS NULL;';
  	EXECUTE 'UPDATE econom.p_profils
  		SET 
  		' || w_field_prefix || '_code = ' || w_tablename || '.code, 
  		' || w_field_prefix || '_texte = ' || w_tablename || '.texte_court
  		FROM econom.' || w_tablename || '
  		WHERE ' || w_tablename || '.oid = p_profils.' || w_field_prefix || '_id
  			AND (' || w_field_prefix || '_code IS DISTINCT FROM ' || w_tablename || '.code OR
  			' || w_field_prefix || '_texte IS DISTINCT FROM ' || w_tablename || '.texte_court);';
  		
  	RETURN 'OK';
  END;