Re: Ayuda con una funcion SQL

From: Calabaza <calalinux(at)gmail(dot)com>
To: pgsql-es-ayuda(at)postgresql(dot)org
Subject: Re: Ayuda con una funcion SQL
Date: 2008-06-23 17:12:35
Message-ID: 958993320806231012n44846af4y10c3759f02f79c70@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

2008/6/20 Juan Carlos Badillo Goy <badillo(at)cav(dot)desoft(dot)cu>:
> Ahora estoy tratando de hacer que una función me retorne lo siguiente: (la
> suma o union por asi decirlo de rs_tabla_1 y rs_tabla_2 )

Eso lo puedes hacer con una vista:

> CREATE OR REPLACE FUNCTION "public"."funct_resultante" (aid_usuario integer)
> RETURNS SETOF "public"."tabla_1" AS
> $body$
> declare rs_tabla_1 tabla_1%rowtype;
> declare rs_tabla_2 tabla_1%rowtype;
> begin
>
> for rs_tabla_1 in
> select * from rs_tabla_1 where propietario = aid_usuario and
> (tipo = 2 or tipo = 7)
> loop
> return next rs_tabla_1;
> end loop;
>
> for rs_tabla_2 in
> select * from rs_tabla_2 where propietario = aid_usuario and tipo
> = 9
> loop
> return next rs_tabla_2;
> end loop;
>
> return;
> end;
> $body$
> LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;

Imagino que las columnas que devuelven ambas tablas son de igual
cantidad y del mismo tipo :

Prueba esto:

Select * from
(
select * from rs_tabla_1 where propietario = 777 and (tipo = 2 or tipo = 7)
) as a
union
(
select * from rs_tabla_2 where propietario = 777 and tipo = 9
) as b

777 = tu identificador de usuario...

Date una vuelta por:
http://www.postgresql.org/docs/8.3/interactive/sql-select.html#SQL-UNION

Un abrazo...
--
§~^Calabaza^~§ from Villa Elisa, Paraguay
----------------
A hendu hína: 1864 - 1949 RICHARD STRAUSS - op.28, Till Eulenspiegels
lustige Striche
http://foxytunes.com/artist/1864+-+1949+richard+strauss/track/op.28%2c+till+eulenspiegels+lustige+striche

In response to

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Marcos Saldivar 2008-06-23 17:28:05 Re: OT: Base de datos de paises de todo el mundo
Previous Message Arturo Munive [pgsql-es-ayuda] 2008-06-23 15:14:08 Re: Duda en necesidad de bloquear tablas