From: Jacobo García <jacobo.garcia@gmail.com>
To: pgsql-sql@postgresql.org
Subject: trigger needs to check in multiple tables.
Date: Fri, 18 Aug 2006 19:17:27 +0200
Message-ID: <5f867bd20608181017u2ccf8db7o12338bbbe66977fb@mail.gmail.com> (raw)
Hello.
I'm running a simple query inside a function that is associated with a
trigger:
SELECT tipo INTO tipocuenta FROM producto WHERE codigo_cuenta=
NEW.codigo_destino;
I am getting this error when running the code on pgadmin III
ERROR: NEW used in query that is not in a rule
QUERY: SELECT tipo FROM producto WHERE codigo_cuenta=NEW.codigo_destino
I don't know what is failing. I'm running postgresql 8.1.4 on windows. Maybe
I have to enable something?
Here is the complete code of the function, sure there will be things wrong,
but the statement failing is just next to BEGIN
CREATE OR REPLACE FUNCTION movimientosenoficina()
RETURNS BOOLEAN AS $$
DECLARE
esoficinacorrecta BOOLEAN;
esfechacorrecta BOOLEAN;
op INTEGER;
fondo INTEGER;
imp INTEGER;
tipocuenta INTEGER;
BEGIN
SELECT tipo INTO tipocuenta FROM producto WHERE codigo_cuenta=
NEW.codigo_destino;
IF (tipocuenta=71) THEN
IF (op=51) THEN
esfechacorrecta:=primerosmes();
esoficinacorrecta:=oficinacorrecta(NEW.codigo,NEW.codigo_oficina
);
ELSIF (NOT op=11 OR NOT op=12)
RAISE EXCEPTION 'Operación no permitida sobre cuentas a plazo';
END IF;
ELSIF (tipocuenta=60)
--Permiten todo tipo de operaciones
ELSIF (tipocuenta=10)
IF (op=54) THEN
SELECT saldo INTO fondo FROM producto WHERE codigo_cuenta =
NEW.destino;
SELECT cantidad INT imp FROM movimiento WHERE codigo =
NEW.codigo;
IF (fondo!=imp) THEN
RAISE EXCEPTION 'Se ha de transferir todo el saldo de la
cuenta'
ELSE
oficinacorrecta(NEW.codigo,NEW.codigo_oficina);
-- Ponemos la cuenta inactiva si retiramos los fondos
UPDATE cuenta_a_plazo SET activo=TRUE WHERE codigo_cuenta =
NEW.destino;
ENDIF;
ENDIF;
ELSE
RAISE EXCEPTION 'Tipo de cuenta no existente';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
/* Este trigger se activa una vez hemos comenzado la transaccion y hemos
insertado en movimiento_oficina
nos sirve para evitar operaciones no definidas o erroneas sobre los
distintos tipos de cuentas*/
CREATE TRIGGER validamovimientosenoficina BEFORE INSERT OR UPDATE ON CLIENTE
FOR EACH ROW EXECUTE PROCEDURE movimientosenoficina();
--
Jacobo García López de Araujo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: jacobo.garcia@gmail.com
Subject: Re: trigger needs to check in multiple tables.
In-Reply-To: <5f867bd20608181017u2ccf8db7o12338bbbe66977fb@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox