I’m trying to implement a trigger to search values on a table, and, if it checks the condtion then insert in another table. The Trigger looks like this:
TRIGGER DISTANCIA
AFTER INSERT ON OBSERVACION FOR EACH ROW
DECLARE
importe_multa NUMBER (3,0);
BEGIN
FOR ROW IN (SELECT fecha_foto, hora_foto, pto_km_radar, sentido_radar, carretera_foto FROM OBSERVACION) LOOP
IF((ROW.fecha_foto = :NEW.fecha_foto)
AND
(ROW.carretera_foto= :NEW.carretera_foto)
AND
(ROW.pto_km_radar=:NEW.pto_km_radar)
AND
(ROW.sentido_radar=:NEW.sentido_radar))
THEN
IF(ROW.hora_foto<:NEW.hora_foto-3,6/86400)
THEN
importe_multa:= (:NEW.hora_foto - ROW.hora_foto - 3,6/86400)*100;
END IF;
IF(:NEW.hora_foto < ROW.hora_foto-3,6/86400)
THEN
importe_multa:= (ROW.hora_foto - :NEW.hora_foto - 3,6/86400)*100;
END IF;
IF(importe_multa IS NOT NULL)
THEN
--Introducimos el valor en el campo.
INSERT INTO SANCION(importe, fecha_foto, hora_foto, coche, tipo)
VALUES
(importe_multa, :NEW.fecha_foto, :NEW.hora_foto, :NEW.coche, '2');
END IF;
END IF;
END LOOP;
END;
When I try to compile the SQLDeveloper software throws this error:
ORA-01008: Not all variables bound.
Am I referencing the variables in OBSERVACION right?
1) A row-level trigger on
OBSERVACIONcannot query thenOBSERVACIONtable. If you do, you’ll get a mutating trigger error. So the approach is fundamentally problematic.2) I would expect that you’re getting the compilation error because you need to represent decimal numbers using the decimal point as the separator, not the comma. The comma is used to separate parameters to a function.
3) If you really want to implement this sort of logic in a trigger rather than putting it in a procedure where it really belongs, you’ll need to work around the mutating trigger error with a series of triggers. This is going to make your code substantially more complex, harder to follow, and harder to debug. But it would allow you to do this using only triggers.