i'm triying to create an autoincrement field (like SERIAL) using a trigger and sequence. I know that only can use a sequence or SERIAL type on field, but i must resolve this using both methods (triggers and secuences)
CREATE SEQUENCE AlimentosSequencia;
CREATE OR REPLACE FUNCTION AlimentoFuncion()
RETURNS "trigger" AS
$BODY$
BEGIN
New.id:=nextval('AlimentosSequencia');
Return NEW;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;
CREATE TRIGGER AlimentosTrigger
BEFORE INSERT
ON alimento
FOR EACH ROW
EXECUTE PROCEDURE AlimentoFuncion();
I try this combination but dosen't works, the table alimento has two fields only, integer id(the autoincrement with trigger and sequence) and the varchar name.
Any suggestion ?
Thanks
nextval()
on the associated sequence of aserial
column for other purposes. No need to complicate things with a trigger. Aside from that, without error message, your question is just noise. Include it verbatim, please. – Erwin Brandstetter Jun 20 '12 at 18:09nextval('AlimentosSequencia')
as the default value for this column, even when it isinteger
. No need of triggers. – Szymon Guz Jun 20 '12 at 18:18