【问题标题】:Merging in trigger with select insert使用选择插入合并触发器
【发布时间】:2013-01-07 22:10:19
【问题描述】:

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

我正在开发一个需要 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 在employment_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 代码之后更改代码。一些指导会帮助我:-)

提前致谢。

【问题讨论】:

  • 为什么用户 1234567 有两个不同的缺席原因?在缺席表/输出中为同一用户设置两行真的有意义吗?

标签: oracle plsql oracle11g


【解决方案1】:

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

我会将合并重写为:

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);

【讨论】:

  • 最后两行:不应该是 INSERT (o.absence_reason, o.employee_id) VALUES (:NEW.absence_reason, S.employee_id) 吗?因为当我尝试你的方式时,我收到“ORA-000926 missing VALUES keyword”错误
猜你喜欢
  • 1970-01-01
  • 2015-02-02
  • 1970-01-01
  • 2013-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
相关资源
最近更新 更多