return: text
lang: plpgsql
parameters:
  p0:
    type: integer
    name: input_rapport_id
strict: STRICT
src: |
  DECLARE 
  	rapport_columns TEXT;
  	rapports_cursor refcursor;
  	numero_rubrique INTEGER;
  	column_name TEXT;
  	rubrique_texte TEXT;
  	table_name TEXT;
  	total_function TEXT;
  	last_rubrique INTEGER;
  BEGIN
  	rapport_columns = '';
  	last_rubrique = 0;
  	OPEN rapports_cursor FOR 
  	SELECT  t_rapports_rubriques.numero_rubrique, t_indicateurs.column_name, t_indicateurs.table_name, t_indicateurs.total_function 
          FROM    activite.t_rapports_rubriques, activite.t_indicateurs
          WHERE   rapport_id = $1 AND indicateur_id = t_indicateurs.oid ORDER BY t_rapports_rubriques.numero_rubrique;
  	FETCH rapports_cursor INTO numero_rubrique, column_name , table_name, total_function;
  	WHILE FOUND LOOP
  		last_rubrique = last_rubrique + 1;
  		FOR i IN last_rubrique..numero_rubrique - 1 LOOP
  			rapport_columns = rapport_columns || ', 0 ' ;	
  			last_rubrique = last_rubrique + 1;
  		END LOOP;
  		IF (table_name != 'SEP') THEN
  			IF (table_name = 'v_rss_1') THEN
  				rapport_columns = rapport_columns || ', ' || total_function || '(' || column_name || ') ' ;
  			END IF;
  			IF (table_name != 'v_rss_1') THEN
  				rapport_columns = rapport_columns || ', ' || total_function || '(indicateur' || numero_rubrique || ') ' ;
  			END IF;
  		END IF;
  		IF (table_name = 'SEP') THEN
  			rapport_columns = rapport_columns || ', 0';
  		END IF;
  		rapport_columns = rapport_columns || ' AS indicateur' || numero_rubrique;
  		FETCH rapports_cursor INTO numero_rubrique, column_name , table_name, total_function;
  	END LOOP;
  	CLOSE rapports_cursor;
          RETURN rapport_columns ;
  END;