I'm doing a sql work but I'm getting trouble with creating a trigger and I have no idea why. Here's the code:
CREATE OR REPLACE TRIGGER artigo_funcionario
BEFORE INSERT ON encomendaartigo
FOR EACH ROW
DECLARE
artigo_invalido exception;
artigo number(3);
auxiliar number(1):=0;
auxiliar_numero number(3);
CURSOR dotacao_funcionario is
select a.artigo_id
from artigo a, familia fa, dotacao d, encomenda e, funcionario f, categoria c
where e.encomenda_id = NEW.encomenda_id
and f.funcionario_id=e.funcionario_id
and f.categoria_id=c.categoria_id
and d.categoria_id=c.categoria_id
and fa.familia_id=d.familia_id
and a.familia_id=fa.familia_id;
BEGIN
open dotacao_funcionario;
loop
fetch dotacao_funcionario into artigo;
EXIT WHEN dotacao_funcionario%notfound;
auxiliar_numero := NEW.artigo_id;
if(artigo = auxiliar_numero) then
auxiliar:=1;
end if;
end loop;
close dotacao_funcionario;
if(auxiliar=0) then
raise artigo_invalido;
end if;
EXCEPTION
WHEN artigo_invalido THEN
dbms_output.put_line('O funcionário não pode encomendar o artigo introduzido');
END;
/
Be aware that the problem is not with the cursor so don't worry about the tables I'm using. the only one that is relevant is the "encomendaartigo" and it has the following attributes: "encomenda_id", "artigo_id".
The problem is that I'm can't quite use the "new.xxxxx" operator. if ran, the compiler log shows the following:
Error(8,7): PL/SQL: SQL Statement ignored
Error(10,30): PL/SQL: ORA-00904: "NEW"."ENCOMENDA_ID": identificador inválido
Error(25,7): PL/SQL: Statement ignored
Error(25,29): PLS-00201: identifier 'NEW.ARTIGO_ID' must be declared
I've tried everything and nothing seems to be working.