0

I'm new in triggers and I'm trying to create a trigger that will be activated before inserting or updating a row in a table. The trigger works when the manager_id (id_cap) has 2 or more employees (id_empleat) or when the sum of the salary of the department is 1000 or more.

This is the table:

Create table empleats (
id_empleat number,
empleat varchar2(200),
id_cap number,
salary number(9,2),
dpto varchar2(20),
constraint empleat_pk primary key (id_empleat),
constraint empleat_con foreign key (id_cap) references empleats(id_empleat) on delete cascade);

The trigger of the manager:

drop trigger supervisor_tr;
CREATE OR REPLACE TRIGGER supervisor_tr
BEFORE INSERT or UPDATE of id_cap on empleats
for each row
DECLARE 
numero number;
pragma autonomous_transaction;
BEGIN
if inserting then 
select count(*) into numero from empleats
where id_cap = :new.id_cap;
if numero >= 2 then
rollback;
RAISE_APPLICATION_ERROR(-20090,'ERROR: El jefe especificado tiene ya 2 empleados a su cargo, introduccion rechazada');
end if;
elsif updating('id_cap') then
select count(*) into numero from empleats
where id_cap = :new.id_cap;
if numero >= 2 then
rollback;
RAISE_APPLICATION_ERROR(-20091,'ERROR: El jefe especificado tiene ya 2 empleados a su cargo, actualizacion rechazada');
end if;
end if;
END;
/

The trigger of the salary:

create or replace trigger presupuesto_1000_tr
before insert or update of salary on empleats
for each row
declare
presupuesto number;
pragma autonomous_transaction;
begin
if updating or inserting then
select sum(salary) into presupuesto from empleats where dpto = :new.dpto;
presupuesto := presupuesto + :new.salary;
if presupuesto >= 1000 then
rollback;
raise_application_error(-20013,'ERROR: EL DEPARTAMENTO SUPERA EL PRESUPUESTO ADMITIDO (1000)');
end if;
end if;
end;
/

and here some of the insert examples I've tried:

insert into empleats values (1,'James',null,500,'HR');
insert into empleats values (2,'Alex',1,2000,'HR');
insert into empleats values (3,'Robin',1,5000,'IT');
insert into empleats values (4,'Pat',1,500,'Accounting');

The problem is that no matter what I insert or update, the triggers don't work. I've tried without the pragma autonomus_transation and only works with inserts (with updates I get the "mutating table error").

4
  • No! do not perform commit/rollback in a trigger. raise the exception and let the caller decide what action to take. please edit the question and show the other table definition Commented May 10, 2020 at 16:54
  • Ok, but if I don't use rollback how can I refuse the insert/update in the database? What table do you mean? Commented May 10, 2020 at 17:01
  • 1
    You "refuse" the insert or update by raising an appropriate exception. The code which is performing the insert or update operation which caused the trigger to be fired is responsible for handling the exception appropriately. Commented May 10, 2020 at 17:38
  • I would echo those who say not to put business logic in a trigger. Just google 'the trouble with triggers', Read this article by Tom Kyte in Oracle Magazine: blogs.oracle.com/oraclemagazine/the-trouble-with-triggers Commented May 10, 2020 at 21:38

1 Answer 1

0

Suggestion: do not use triggers to implement business logic such as what you're trying to do here. Having "magic code" buried in a trigger doing important work out-of-line makes your code more difficult to understand. Instead, create procedures which perform the insert or update operations which are needed along with the appropriate validation logic, and use those procedures whenever they are needed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.