我之前发过这个问题。但现在我的项目经理回来给了我一套新的指示。再一次,我现在有点迷失,并试图在某种程度上修复它。
我正在研究一个需要INSERT INTO和WHERE逻辑的触发器。
我有三张桌子。
Absence_table:
-----------------------------
| user_id | absence_reason |
-----------------------------
| 1234567 | 40 |
| 1234567 | 50 |
| 1213 | 40 |
| 1314 | 20 |
| 1111 | 20 |
-----------------------------
company_table:
-----------------------------
| user_id | company_id |
-----------------------------
| 1234567 | 10201 |
| 1213 | 10200 |
| 1314 | 10202 |
| 1111 | 10200 |
-----------------------------
employment_table:
-------------------------------------------
| user_id | emp_type | employee_id |
-------------------------------------------
| 1234567 | Int | 1 |
| 1213 | Int | 2 |
| 1314 | Int | 3 |
| 1111 | Ext | 4 |
-------------------------------------------
最后我得出的数据表只有谁在jobs_table中有emp_type = Int并且有company_id = 10200
出:
-------------------------------------------
| employee_id | absence_reason | user_id |
-------------------------------------------
| 1 | 40 | 1234567 |
| 1 | 50 | 1234567 |
| 2 | 40 | 1213 |
| 3 | 20 | 1314 |
-------------------------------------------
这是我的触发器:
CREATE OR REPLACE TRIGGER "INOUT"."ABSENCE_TRIGGER"
AFTER INSERT ON arc_hrcs.absences_data
FOR EACH ROW
DECLARE
BEGIN
CASE
WHEN UPDATING THEN
MERGE INTO out o USING DUAL ON (out.user_id =:NEW.user_id)
WHEN MATCHED THEN UPDATE SET
out.employee_id = (SELECT employee_id FROM employment_table WHERE user_id = :NEW.user_id),
out.absence_reason = :NEW.absence_reason,
out.user_id = :NEW.user_id
WHEN NOT MATCHED THEN
insert into out (absence_reason, employee_id)
select :NEW.absence_reason, e.employee_id
from employment_table e
inner join company_table c
on c.user_id = e.user_id
where e.user_id = :NEW.user_id
and e.emp_type = 'INT'
and c.company_id = '10200';
END CASE;
END absence_trigger;
根据合并语法,我无法弄清楚如何在不匹配那么代码之后更改代码。一些指南可以帮助我: - )
提前致谢。
答案 0 :(得分:2)
我之前的回答有点太快了。
我会将合并重写为:
MERGE INTO out o USING (select e.employee_id, :NEW.user_id as user_id
from employment_table e
inner join company_table c
on c.user_id = e.user_id
where e.user_id = :NEW.user_id
and e.emp_type = 'INT'
and c.company_id = '10200') S
ON (out.user_id =S.user_id)
WHEN MATCHED THEN UPDATE SET
out.employee_id = s.employee_id,
out.absence_reason = :NEW.absence_reason,
out.user_id = :NEW.user_id
WHEN NOT MATCHED THEN
insert (absence_reason, employee_id)
values (:NEW.absence_reason, S.employee_id);