简体   繁体   English

在 Oracle SQL 开发人员中使用触发器

[英]Working with triggers in Oracle SQL developer

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.当 manager_id (id_cap) 有 2 个或更多员工 (id_empleat) 或部门的工资总和为 1000 或更多时,触发器起作用。

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").我已经尝试不使用 pragma autonomus_transation 并且仅适用于插入(通过更新我得到“变异表错误”)。

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.相反,创建执行插入或更新操作的过程以及适当的验证逻辑,并在需要时使用这些过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM