0

这个问题我之前发过。但现在我的项目经理回来了,给了我一套新的指示。同样,我现在有点迷茫,并试图在某种程度上修复它。

我正在研究一个需要 INSERT INTO 和 WHERE 逻辑的触发器。

我有三张桌子。

缺席表:

-----------------------------
|  user_id | absence_reason |
-----------------------------
|  1234567 |   40           |
|  1234567 |   50           |
|  1213    |   40           |
|  1314    |   20           |
|  1111    |   20           |
-----------------------------

公司表:

-----------------------------
| user_id  | company_id     |
-----------------------------
| 1234567  |  10201         |
| 1213     |  10200         |
| 1314     |  10202         |
| 1111     |  10200         |
-----------------------------

就业表:

-------------------------------------------
| user_id  |   emp_type    |  employee_id |
-------------------------------------------
| 1234567  |   Int         |    1         |
| 1213     |   Int         |    2         |   
| 1314     |   Int         |    3         |
| 1111     |   Ext         |    4         |
-------------------------------------------

最后我列出了只有emp_type = Int inemployment_table 和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;

我不知道如何根据合并语法在 WHEN NOT MATCHED THEN 代码之后更改代码。一些指导会帮助我:-)

提前致谢。

4

1 回答 1

3

我之前的回答有点太快了。

我会将合并重写为:

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);
于 2013-01-07T12:30:57.987 回答