Pessoal, sei que deve haver outras formas de fazer isso (xml, array, etc.),
mas achei pratico, e é assim que fazendo. Da ferramenta RAD que uso (Visual
Studio), monto uma ou varias strings com o conteudo que quero passar
(exemplo, cheques de um recebimento), e a função distrincha isso e gera um
loop de registros.
Assim, eu passo como parametro, por exemplo, o seguinte:
str = '1;;50;;TESTE[#$]2;;70.15;;TESTE2[#$]3;;25.8;;TESTE3'
e leio da seguinte forma
for reg in select * from sp_texto_to_record(str)
loop
x = reg.c1::integer;
y = reg.c2::numeric;
z = reg.c3;
end loop;
A função abaixo está preparada para receber 20 campos por registro.
CREATE OR REPLACE FUNCTION "public"."sp_texto_to_record" (ptexto text, out
c1 text, out c2 text, out c3 text, out c4 text, out c5 text, out c6 text,
out c7 text, out c8 text, out c9 text, out c10 text, out c11 text, out c12
text, out c13 text, out c14 text, out c15 text, out c16 text, out c17 text,
out c18 text, out c19 text, out c20 text) RETURNS SETOF record AS
$body$
DECLARE
linha integer;
campo integer;
texto text;
texto_linha text;
BEGIN
linha = 1;
if trim(ptexto) = '' then
return;
end if;
texto = ptexto;
while (strpos(texto,';;;;') > 0)
loop
texto = replace(texto,';;;;',';; ;;');
end loop;
while (strpos(texto,';;#$') > 0)
loop
texto = replace(texto,';;#$',';; #$');
end loop;
if (substr(texto,length(texto) - 1,2) = ';;') then
texto = texto || ' ';
end if;
while split_part(texto,'#$',linha) <> ''
loop
c1 = null;
c2 = null;
c3 = null;
c4 = null;
c5 = null;
c6 = null;
c7 = null;
c8 = null;
c9 = null;
c10 = null;
c11 = null;
c12 = null;
c13 = null;
c14 = null;
c15 = null;
c16 = null;
c17 = null;
c18 = null;
c19 = null;
c20 = null;
texto_linha = split_part(texto,'#$',linha);
campo = 1;
while split_part(texto_linha,';;',campo) <> ''
loop
if campo = 1 then
c1 = trim(split_part(texto_linha,';;',campo));
elseif campo = 2 then
c2 = trim(split_part(texto_linha,';;',campo));
elseif campo = 3 then
c3 = trim(split_part(texto_linha,';;',campo));
elseif campo = 4 then
c4 = trim(split_part(texto_linha,';;',campo));
elseif campo = 5 then
c5 = trim(split_part(texto_linha,';;',campo));
elseif campo = 6 then
c6 = trim(split_part(texto_linha,';;',campo));
elseif campo = 7 then
c7 = trim(split_part(texto_linha,';;',campo));
elseif campo = 8 then
c8 = trim(split_part(texto_linha,';;',campo));
elseif campo = 9 then
c9 = trim(split_part(texto_linha,';;',campo));
elseif campo = 10 then
c10 = trim(split_part(texto_linha,';;',campo));
elseif campo = 11 then
c11 = trim(split_part(texto_linha,';;',campo));
elseif campo = 12 then
c12 = trim(split_part(texto_linha,';;',campo));
elseif campo = 13 then
c13 = trim(split_part(texto_linha,';;',campo));
elseif campo = 14 then
c14 = trim(split_part(texto_linha,';;',campo));
elseif campo = 15 then
c15 = trim(split_part(texto_linha,';;',campo));
elseif campo = 16 then
c16 = trim(split_part(texto_linha,';;',campo));
elseif campo = 17 then
c17 = trim(split_part(texto_linha,';;',campo));
elseif campo = 18 then
c18 = trim(split_part(texto_linha,';;',campo));
elseif campo = 19 then
c19 = trim(split_part(texto_linha,';;',campo));
elseif campo = 20 then
c20 = trim(split_part(texto_linha,';;',campo));
end if;
campo = campo + 1;
end loop;
linha = linha + 1;
return next;
end loop;
return;
END;
$body$
LANGUAGE 'plpgsql'
VOLATILE
CALLED ON NULL INPUT
SECURITY INVOKER
COST 100 ROWS 1000;
--
View this message in context:
http://www.nabble.com/Dica-para-passar-registros-para-uma-fun%C3%A7%C3%A3o-tp23915141p23915141.html
Sent from the PostgreSQL - Brasil mailing list archive at Nabble.com.
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral