Re: [pgsql-es-ayuda] Re: [pgsql-es-ayuda] Ayuda con función retornando registros

From: Miguel Angel Hernandez Moreno <miguel(dot)hdz(dot)mrn(at)gmail(dot)com>
To: Dario Andres Almonte Alonzo <triby25(at)gmail(dot)com>
Cc: Juan <smalltalker(dot)marcelo(at)gmail(dot)com>, pgsql-es-ayuda <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: Re: [pgsql-es-ayuda] Re: [pgsql-es-ayuda] Ayuda con función retornando registros
Date: 2013-09-05 16:05:50
Message-ID: CAGYOd3rp_FLe1Pn+LSbovQNtxS0AXgLf9bz+ERku5FnoHM1CBA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

Si retornas la tabla

CREATE FUNCTION getDatos()
RETURNS NOMBRE_TABLA
LANGUAGE sql
AS $_$
select id, texto from prueba;
$_$;

Aqui yo uso el lenguaje SQL y no el PLPGSQL

El 5 de septiembre de 2013 10:42, Dario Andres Almonte Alonzo <
triby25(at)gmail(dot)com> escribió:

> Si esa opción ya la había visto.
>
> Lo que quería saber es si se podía retornar la tabla sin tener que definir
> nada, igual que en SQL Server para ahorrar algo de tiempo ya que son
> muchísimas funciones.
>
>
>
> El 5 de septiembre de 2013 11:39, Juan <smalltalker(dot)marcelo(at)gmail(dot)com>escribió:
>
> Holas
>>
>>
>> Se me ocurrre si creas un tipo con los nombres y tipos de tu salida,
>> podriasa poner as setof MyTypo
>> y problema solucionado, ojo!!! especulo, no lo probé.
>>
>> salu2
>> jmdc
>>
>> 2013/9/5 Dario Andres Almonte Alonzo <triby25(at)gmail(dot)com>:
>> > A eso es que me refería, siempre tengo que indicar cada columna que voy
>> a
>> > retornar de una u otra forma y es bastante tedioso cuando tienes
>> funciones
>> > que retornan hasta 50 columnas.
>> >
>> > A lo que me refiero es si no hay forma de evitar eso como en SQL server
>> > donde solo haces:
>> >
>> > create funtion prueba(codcia varchar(2))
>> > returns table
>> > as
>> > return(select t1.campo01,t2.campo05 from t1 inner join t2 on ti.id
>> =t2.id2)
>> >
>> >
>> > en postgres obligatoriamente tendria que hacer:
>> >
>> > create or replace funtion prueba(codcia varchar(2))
>> > returns table(campo01 integer,campo05 text)
>> > as
>> > $BODY$
>> > return query select t1.campo01,t2.campo05 from t1 inner join t2 on
>> > ti.id=t2.id2;
>> > $BODY$
>> > LANGUAGE plpgsql VOLATILE
>> > COST 100;
>> >
>> >
>> > o me puedo evitar la definición de columnas???
>> >
>> >
>> > El 5 de septiembre de 2013 11:24, Miguel Angel Hernandez Moreno
>> > <miguel(dot)hdz(dot)mrn(at)gmail(dot)com> escribió:
>> >
>> >> Hola
>> >>
>> >> creao que tienes que hacer el
>> >>
>> >> select * from act_depre_cuentas('01');
>> >> as
>> >> (campo01 tipo_dato01, campo02 tipo_dato02, ... , campoN tipo_datoN)
>> >>
>> >>
>> >> o lo que puedes hacer es
>> >>
>> >>
>> >>
>> >> CREATE OR REPLACE FUNCTION act_depre_cuentas(codcia character
>> varying(2))
>> >> RETURNS TABLE(campo01 tipo_dato01, campo02 tipo_dato02, ... , campoN
>> >> tipo_datoN) AS
>> >>
>> >>
>> >> ...................
>> >> ......................
>> >>
>> >> end;
>> >> $BODY$
>> >> LANGUAGE plpgsql VOLATILE
>> >> COST 100;
>> >>
>> >>
>> >>
>> >> Asi lo resolvi alguna vez, espero te pueda servir
>> >>
>> >> Saludos
>> >>
>> >>
>> >>
>> >> El 5 de septiembre de 2013 10:19, Dario Andres Almonte Alonzo
>> >> <triby25(at)gmail(dot)com> escribió:
>> >>
>> >>> buen día lista
>> >>>
>> >>> estoy migrando una base de datos de sql server 2005 a postgres
>> 9.2.4,ya
>> >>> he pasando las funciones que retornan valores escalares sin ningún
>> problema.
>> >>>
>> >>> ahora estoy empezando a migrar las funciones que retornan conjuntos de
>> >>> registros y me he encontrado con un problema, me dice que debo
>> definir cada
>> >>> columna que voy a retornar.
>> >>>
>> >>> esta es la primera función que intente migrar:
>> >>>
>> >>> CREATE OR REPLACE FUNCTION act_depre_cuentas(codcia character
>> varying(2))
>> >>> RETURNS setof record AS
>> >>> $BODY$
>> >>> begin
>> >>>
>> >>> return query SELECT codcia_hed,substring(referen1_hed from 11 for
>> 6) as
>> >>> codart_act, cnttran.idTran_hed, cnttran.modulo_hed,
>> cnttran.codent_hed,
>> >>> cnttran.referen1_hed,
>> >>> cnttran.fecha_hed, cnttran.concep_hed, cnttran.monto_hed,
>> >>> cntdeta.numcta_det,
>> >>> cntdeta.centro_det, cntdeta.debito_det,
>> >>> cntdeta.credito_det, cntdeta.debcre_det,
>> >>> (select nomcta_cat from cntcatal where codcia_cat=codcia_hed and
>> >>> numcta_cat=numcta_det) as cuenta,
>> >>> (select descri_act from actmaes where codcia_act=codcia_hed and
>> >>> codart_act= substring(referen1_hed from 11 for 6)) as activo,
>> >>> (select descri_dep from actdepa where codcia_dep=codcia_hed and
>> >>> coddep_dep=(select coddep_act from actmaes where
>> codcia_act=codcia_hed and
>> >>> codart_act= substring(referen1_hed from 11 for 6)) ) as departamento,
>> >>> (select descri_tip from acttipo where codcia_tip=codcia_hed and
>> >>> tipact_tip=(select tipart_act from actmaes where
>> codcia_act=codcia_hed and
>> >>> codart_act= substring(referen1_hed from 11 for 6)))as tipo,
>> >>> (select coddep_act from actmaes where codcia_act=codcia_hed and
>> >>> codart_act= substring(referen1_hed from 11 for 6)) as coddep_act,
>> >>> (select tipart_act from actmaes where codcia_act=codcia_hed and
>> >>> codart_act= substring(referen1_hed from 11 for 6)) as tipart_act
>> >>> FROM cntdeta INNER JOIN
>> >>> cnttran ON cntdeta.codcia_det =
>> cnttran.codcia_hed
>> >>> AND cntdeta.idTrand_det = cnttran.idTran_hed
>> >>> where modulo_hed='ACT' and codent_hed='C' and codcia_hed = codcia;
>> >>>
>> >>> end;
>> >>> $BODY$
>> >>> LANGUAGE plpgsql VOLATILE
>> >>> COST 100;
>> >>>
>> >>> cuando la creo el resultado es: Query returned successfully with no
>> >>> result in 13 ms.
>> >>>
>> >>> pero cuando la llamo:
>> >>> select * from act_depre_cuentas('01');
>> >>>
>> >>> el resultado es:
>> >>> ERROR: la lista de definición de columnas es obligatoria para
>> funciones
>> >>> que retornan «record»
>> >>> SQL state: 42601
>> >>> Character: 15
>> >>>
>> >>> He estado buscando en foros y me dicen que debo definir cada
>> parámetro en
>> >>> el RETURNS o crear un type personalizado con todos los registros que
>> quiero
>> >>> retornar.
>> >>>
>> >>> Hacer esto me tomaría muchísimo tiempo ya que tengo funciones que
>> >>> retornan querys con muchísimas columnas.
>> >>>
>> >>> Mi pregunta es,¿Hay alguna forma de retornar el query sin tener que
>> >>> definir todas las columnas??
>> >>
>> >>
>> >>
>> >>
>> >> --
>> >> ISC Miguel Angel Hernandez Moreno
>> >
>> >
>>
>
>

--
ISC Miguel Angel Hernandez Moreno

In response to

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Dario Andres Almonte Alonzo 2013-09-05 19:59:04 Re: [pgsql-es-ayuda] Re: [pgsql-es-ayuda] Ayuda con función retornando registros
Previous Message Arcel Labrada Batista 2013-09-05 16:03:44 Re: Re: [pgsql-es-ayuda] Re: [pgsql-es-ayuda] Ayuda con función retornando registros