Обсуждение: trigger needs to check in multiple tables.

Поиск
Список
Период
Сортировка

trigger needs to check in multiple tables.

От
"Jacobo García"
Дата:
Hello.<br /><br />I'm running a simple query inside a function that is associated with a trigger:<br /><br />    <span
style="font-family:courier new,monospace;">SELECT tipo INTO tipocuenta FROM producto WHERE
codigo_cuenta=NEW.codigo_destino;</span><br /><br />I am getting this error when running the code on pgadmin III<br
/><br/><span style="font-family: courier new,monospace;">ERROR:  NEW used in query that is not in a rule</span><br
style="font-family:courier new,monospace;" /><span style="font-family: courier new,monospace;">QUERY:  SELECT  tipo
FROMproducto WHERE codigo_cuenta=NEW.codigo_destino</span><br /><br />I don't know what is failing. I'm running
postgresql8.1.4 on windows. Maybe I have to enable something? <br /><br />Here is the complete code of the function,
surethere will be things wrong, but the statement failing is just next to BEGIN<br /><br /><span style="font-family:
couriernew,monospace;">CREATE OR REPLACE FUNCTION movimientosenoficina() <br />    RETURNS BOOLEAN AS $$<br /><br />   
DECLARE<br/><br />    esoficinacorrecta     BOOLEAN;<br />    esfechacorrecta       BOOLEAN;<br />     op           
 INTEGER;   <br />    fondo             INTEGER;<br />    imp             INTEGER; <br />    tipocuenta       
 INTEGER;<br/>    <br />    BEGIN<br /><br />    SELECT tipo INTO tipocuenta FROM producto WHERE
codigo_cuenta=NEW.codigo_destino;<br/><br />    IF (tipocuenta=71) THEN<br />        <br />        IF (op=51) THEN <br
/>           esfechacorrecta:=primerosmes();<br />           
esoficinacorrecta:=oficinacorrecta(NEW.codigo,NEW.codigo_oficina);<br/><br />        ELSIF (NOT op=11 OR NOT op=12)<br
/>   <br />            RAISE EXCEPTION 'Operación no permitida sobre cuentas a plazo'; <br />        <br />        END
IF;<br/><br />    ELSIF (tipocuenta=60)<br />    <br />            <br />        --Permiten todo tipo de operaciones
<br/><br /><br /><br />    ELSIF (tipocuenta=10)<br />    <br />        <br /><br />        IF (op=54) THEN <br /><br
/>           SELECT saldo INTO fondo FROM producto WHERE codigo_cuenta = NEW.destino;<br />            SELECT cantidad
INTimp FROM movimiento WHERE codigo = NEW.codigo;<br /><br />            IF (fondo!=imp) THEN <br />        <br />   
           RAISE EXCEPTION 'Se ha de transferir todo el saldo de la cuenta'<br />        <br />            ELSE<br
/>               oficinacorrecta(NEW.codigo,NEW.codigo_oficina);<br />            <br />                -- Ponemos la
cuentainactiva si retiramos los fondos <br />                UPDATE cuenta_a_plazo SET activo=TRUE WHERE codigo_cuenta
=NEW.destino;<br />            ENDIF;<br />        ENDIF;<br /><br />        <br /><br /><br />    ELSE<br />       
RAISEEXCEPTION 'Tipo de cuenta no existente'; <br /><br /><br />    END IF;<br /><br /><br />    <br /><br />    <br
/><br/><br />    RETURN NEW;<br /><br /><br />    END;<br />$$ LANGUAGE plpgsql;<br /><br />/* Este trigger se activa
unavez hemos comenzado la transaccion y hemos insertado en movimiento_oficina <br />  nos sirve para evitar operaciones
nodefinidas o erroneas sobre los distintos tipos de cuentas*/<br /><br />CREATE TRIGGER validamovimientosenoficina
BEFOREINSERT OR UPDATE ON CLIENTE<br />FOR EACH ROW EXECUTE PROCEDURE movimientosenoficina(); </span><br clear="all"
/><br/>-- <br />Jacobo García López de Araujo  

Re: trigger needs to check in multiple tables.

От
Michael Fuhr
Дата:
On Fri, Aug 18, 2006 at 07:17:27PM +0200, Jacobo Garca wrote:
> 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

Trigger functions must return TRIGGER; the function you posted
returns BOOLEAN.  If you change the return type to TRIGGER then the
above error should go away.  However, the function also has several
syntax errors.

>        ELSIF (NOT op=11 OR NOT op=12)

The above line and a few others are missing THEN.

>            SELECT cantidad INT imp FROM movimiento WHERE codigo => NEW.codigo;

INT should be INTO.

>                RAISE EXCEPTION 'Se ha de transferir todo el saldo de la cuenta'

This line is missing a trailing semicolon.

>                oficinacorrecta(NEW.codigo,NEW.codigo_oficina);

Use PERFORM to call a function for its effects and ignore its return
value.  Or did you mean to assign the return value to a variable?

>            ENDIF;

ENDIF should be END IF.

If you make the indicated changes then the function should be created
successfully.  I didn't look closely at the logic, so whether it'll
actually work is another matter ;-)

-- 
Michael Fuhr