[Pgsql-ayuda] Crear funcion.

Alvaro Herrera alvherre@dcc.uchile.cl
Thu, 29 May 2003 22:16:56 -0400


On Thu, May 29, 2003 at 06:44:33PM -0400, Leonardo Boet Sánchez wrote:
> Buenas tardes, soy nuevo en el mundo de postgres.
> quisiera crear una funcion que me retorne un conjunto de registro. lo trato de hacer con el psql 

test=> create table autor (id serial primary key, nombre text);
NOTICE:  CREATE TABLE will create implicit sequence 'autor_id_seq' for SERIAL column 'autor.id'
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 'autor_pkey' for table 'autor'
CREATE TABLE
test=> create table libro (id serial primary key, id_autor int4 references autor,
titulo text);
NOTICE:  CREATE TABLE will create implicit sequence 'libro_id_seq' for SERIAL column 'libro.id'
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 'libro_pkey' for table 'libro'
NOTICE:  CREATE TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
CREATE TABLE
test=> create function muestra_libros () returns setof record as 'select
nombre, titulo from autor join libro on (id_autor=autor.id)' language sql;
CREATE FUNCTION
test=> select * from muestra_libros() as foo(autor text, titulo text);
     autor     |              titulo
---------------+----------------------------------
 Don Knuth     | The Art of Computing Programming
 J.R.R.Tolkien | The Hobbit
 J.R.R.Tolkien | The Lord of the Rings
(3 rows)

-- 
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"Some men are heterosexual, and some are bisexual, and some
men don't think about sex at all... they become lawyers" (Woody Allen)