!Good!
I am doing this exercise:
When inserting or modifying a vendor, make sure that the id does not match any customer id. If it matches, it will be inserted, but it will be recorded in a new table as a log.
Where the relational model looks like this:
Clientess (id, nombre)
Vendedores (id, nombre)
Ventas (id_clienteFK, id_vendedorFK, importe, fecha)
tabla_log(id,tipo,descripción)
And trying to perform the exercise, I'm trying with this solution:
CREATE OR REPLACE TRIGGER insertar_vendedor BEFORE INSERT OR UPDATE
ON vendedores
DECLARE
idCliente number;
BEGIN
select id into idCliente from clientess where clientess.id = :new.id;
if (idCliente%FOUND) then
INSERT INTO tabla_log values (1,'insertar','Se ha insertado un vendedor');
ELSIF (idCliente%NOTFOUND) THEN
DBMS_OUTPUT.PUT_LINE('No hay conincidencia');
END if;
END;
But this error has popped up, and the truth is that I'm starting with Triggers and I'm pretty lost... Let's see if someone knows how to structure this exercise.
Mistake:
I think you should use FOR EACH ROW in your trigger so that it's done at the row level, i.e. so that it fires every time you try to insert or update.
I edit because asking for FOUND or NOTFOUND in a
SELECT INTO
simply doesn't do anything. If there are no matches, it will throw an exception, which is what would be handled in the exception block. [answer on SO in English]