retornar valor en función

From: José Fermín francisco ferreras <josefermin54(at)hotmail(dot)com>
To: pgsql-es-ayuda(at)postgresql(dot)org
Subject: retornar valor en función
Date: 2006-04-20 16:35:13
Message-ID: BAY23-F492C455AC808711541A73C6BA0@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

Hola muchachos, tengo una función q se encarga d generar el codigo d una
tabla, pero he tenido ciertos problemas ya q si la tabla esta vacia tira un
error d q el campo id no puede dejarse en blanco.
Pero si la tabla ya tiene valores funciona a la perfección.

Espero q algunos d ustedes me pueda dar una mano con este problema.

Esta es la función:

CREATE OR REPLACE FUNCTION mifuncion()
RETURNS int4 AS
$BODY$
declare
cod integer;
begin
select into cod max(id)
from empleados;

return (cod + 1);
end;
$BODY$
LANGUAGE 'plpgsql' STABLE;
ALTER FUNCTION mifuncion() OWNER TO postgres;

Y esta es la tabla:

CREATE TABLE empleados
(
id int4 NOT NULL DEFAULT mifuncion(),
nombre varchar(20),
id_emp int4,
CONSTRAINT empleados_pkey PRIMARY KEY (id),
CONSTRAINT empleados_id_emp_fkey FOREIGN KEY (id_emp)
REFERENCES empresas (id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE SET NULL
)
WITH OIDS;
ALTER TABLE empleados OWNER TO postgres;

<html><div>ing. José Fermín Francisco Ferreras <BR>San Francisco de Macorís,
Rep. Dom. <BR></div></html>

_________________________________________________________________
Consigue aquí las mejores y mas recientes ofertas de trabajo EE.UU.
http://latino.msn.com/empleos

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Espartano 2006-04-20 16:38:07 Re: Pregunta
Previous Message Juan Martínez 2006-04-20 16:30:32 Re: Pregunta