Re: Crear Funciones en PG

From: Guillermo Villanueva <guillermovil(at)gmail(dot)com>
To: Aland Laines <aland(dot)laines(at)gmail(dot)com>
Cc: pgsql-es-ayuda <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: Re: Crear Funciones en PG
Date: 2012-08-30 02:39:29
Message-ID: CANm+PCAHf0wBASYxDHK_vBXts0wBfk=KjEpGGsSAuLfDn3F_iA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

Mirá, donde dice:
maximo =(select max("IdeReg") from "Publicacion")::integer;
if(maximo is null) then
maximo=0;
end if;

Yo lo harías así:
select coalesce(max(IdeReg),0) into maximo from Publicacion

Y donde dice:
maximo=maximo+1;

reemplazalo por
maximo := maximo + 1;

Y quizá sería bueno también hacer lo que te dice Aland

Guillermo Villanueva

2012/8/20 Aland Laines <aland(dot)laines(at)gmail(dot)com>

> en el ejemplo que puse como seria eso Guillermo?
>
> Gracias;
>
> *Aland Laines Calonge*
> Twitter: @lainessolutions
> http://about.me/aland.laines
>
>
>
>
> 2012/8/20 Guillermo Villanueva <guillermovil(at)gmail(dot)com>
>
>> La asignación en plpgsql es con :=
>> Guillermo Villanueva
>>
>>
>>
>>
>> 2012/8/6 Aland Laines <aland(dot)laines(at)gmail(dot)com>
>>
>>>
>>> Hola a todos, hace poco que estoy usando postgres.
>>>
>>> Estoy viendo lo de funciones y quise crear esta:
>>>
>>> ----------------------------
>>> CREATE OR REPLACE FUNCTION adecuar_publicacion() RETURNS boolean as
>>> $BODY$
>>> DECLARE
>>> maximo integer;
>>> BEGIN
>>> maximo =(select max("IdeReg") from "Publicacion")::integer;
>>> if(maximo is null) then
>>> maximo=0;
>>> end if;
>>> maximo=maximo+1;
>>> DROP SEQUENCE "Publicacion_IdeReg_seq";
>>> CREATE SEQUENCE "Publicacion_IdeReg_seq"
>>> INCREMENT 1
>>> MINVALUE 1
>>> MAXVALUE 9223372036854775807
>>> * START maximo*
>>> CACHE 1;
>>> ALTER TABLE "Publicacion_IdeReg_seq"
>>> OWNER TO postgres;
>>> ALTER TABLE "Publicacion" ADD CONSTRAINT "IdeReg" PRIMARY KEY("IdeReg" );
>>> ALTER TABLE "Publicacion" alter column "IdeReg" set default
>>> nextval('"Publicacion_IdeReg_seq"'::regclass);
>>> UPDATE "Publicacion" set "ANULADO" = null WHERE "ANULADO" = '';
>>> UPDATE "Publicacion" set "NroCar" = null WHERE "NroCar" = '';
>>> UPDATE "Publicacion" set genero = null WHERE genero = '';
>>> if found then
>>> return true;
>>> else
>>> return false;
>>> end if;
>>> END;
>>> $BODY$
>>> LANGUAGE 'plpgsql';
>>> -------------------------------------------------------
>>>
>>>
>>> El problema esta en la linea *START maximo* me dice que tengo un
>>> error de sintaxis, quiero saber como pasarle el valor de la variable *
>>> maximo* para que no me arroje error de sintaxis, gracias por su ayuda.
>>>
>>> *Aland Laines Calonge*
>>> Twitter: @lainessolutions
>>> http://about.me/aland.laines
>>>
>>>
>>>
>>
>

In response to

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Alvaro Herrera 2012-08-30 02:55:58 Re: Crear Funciones en PG
Previous Message FABIO ARIAS 2012-08-30 00:11:25 Re: ES, Lista te invita a registrarte en Lenddo