Resolvido

Pessoal, com a ajuda dos colegas cheguei a seguinte função

CREATE OR REPLACE FUNCTION femailsclientes(integer)
  RETURNS character varying AS
$BODY$
  declare
    emails character varying;
    reg record;
begin
    emails = '';
    for reg in select email from mv_clientes_emails where (cod_id = $1)
    loop
      emails = reg.email || ', ' || emails;
    end loop;
  return left(emails, length(emails)-2); -- Retira a ultima virgula
end;
$BODY$
  LANGUAGE plpgsql IMMUTABLE
  COST 100;
ALTER FUNCTION femailsclientes(integer)
  OWNER TO postgres;


Show de bola obrigado a todos


Marcelo Silva
------------------------------------------------


Em 8 de abril de 2013 10:08, Fábio Telles Rodriguez
<[email protected]>escreveu:

> Ao invés de usar um LOOP  que pode sair bem caro, você pode utilizar
> ARRAYs que são bem eficientes. Veja o exemplo:
>
> CREATE TABLE teste (abizi varchar);
> INSERT INTO teste VALUES ('AAA'),('BBB'),('CCC'),('DDD');
> SELECT array_to_string(array_agg(abizi),',') FROM teste;
>
> Aqui usei duas funções com array, uma de aggregação: array_agg[1] e outra
> para transformar o array numa string, o array_to_string[2].
>
> [1] http://www.postgresql.org/docs/current/static/functions-aggregate.html
> [2] http://www.postgresql.org/docs/current/static/functions-array.html
>
> OBS: Se for usar uma função mesmo, lembre-se de declarar como IMMUTABLE e
> não como VOLATILE. Assim o desempenho da sua função melhora muito.
>
> []s
>
>
>
> 2013/4/8 Marcelo da Silva <[email protected]>
>
>> Pessoal estou tentando concatenar numa string o resultado de um select:
>>
>> email1, email2, email3, etc
>>
>> Sei pouca coisa em termos de funções em Postgres, criei a função abaixo
>> pra isso.
>> Mas está faltando o principal que é concatenar, vejam:
>>
>> CREATE OR REPLACE FUNCTION femailsclientes(integer)
>>   RETURNS character varying AS
>> $BODY$
>>   declare
>>     result character varying;
>> begin
>>
>>     for result in select distinct email from mv_clientes_emails where
>> (cod_id = $1)
>>     loop
>>       result = result || ',' || result;
>>     end loop;
>>   return result;
>>
>> end;
>> $BODY$
>>   LANGUAGE plpgsql VOLATILE
>>   COST 100;
>> ALTER FUNCTION femailsclientes(integer)
>>   OWNER TO postgres;
>>
>> Podem me ajudar?
>>
>> Obrigado
>> ...............................
>>
>> Marcelo Silva
>>
>>
>> _______________________________________________
>> pgbr-geral mailing list
>> [email protected]
>> https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral
>>
>>
>
>
> --
> Atenciosamente,
> Fábio Telles Rodriguez
> blog: http:// 
> <http://www.midstorm.org/~telles/>s<http://tellesr.wordpress.com/>
> avepoint.blog.br
> e-mail / gtalk / MSN: [email protected]
> Skype: fabio_telles
>
> Timbira - A empresa brasileira de Postgres
> http://www.timbira.com.br
>
> _______________________________________________
> pgbr-geral mailing list
> [email protected]
> https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral
>
>
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a