CREATE OR REPLACE FUNCTION public.concat_selected_fields(text, text, text default 'public') 
-- concatene les lignes d'un select unique dans une seule chaine de caractère
RETURNS TEXT AS $$
DECLARE 
	colname ALIAS FOR $1;
	tablename ALIAS FOR $2;
	schemaname ALIAS FOR $3;
	result text := '';
	res_distinct RECORD;
	cmd varchar := '';
BEGIN
	cmd := 'SELECT DISTINCT '
	|| ' cast(' || quote_ident(colname) || ' as TEXT)' 
	|| ' AS f1 FROM  '
	|| quote_ident(schemaname)
	|| '.'
	|| quote_ident(tablename) 
	|| ' ORDER BY f1 ' ;

	-- RAISE NOTICE '%', cmd ;

	FOR res_distinct IN EXECUTE cmd LOOP
	result := result || cast(res_distinct.f1 as TEXT) || ' ';
	END LOOP;
	RETURN result;
END
$$ LANGUAGE plpgsql;


