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    |  emp_no |
--------------------------------------
| 1234567  |   Int         |    1    |
| 1213     |   Int         |    2    |   
| 1314     |   Int         |    3    |
| 1111     |   Ext         |    4    |
--------------------------------------

最后我列出了只有emp_type = Int inemployment_table 和company_id = 10200 的数据应该去哪里的表格

出去:

--------------------------------
| employee_id | absence_reason |
--------------------------------
|  1          |    40          |
|  1          |    50          |
|  2          |    40          |
|  3          |    20          |
--------------------------------

这是我的触发器:

CREATE OR REPLACE TRIGGER "INOUT"."ABSENCE_TRIGGER" 
  AFTER INSERT ON absence_table 
  FOR EACH ROW
DECLARE
BEGIN
  CASE
      WHEN INSERTING THEN
           INSERT INTO out (absence_reason, employee_id)
           VALUES (:NEW.absence_reason, (SELECT employee_id FROM employment_table WHERE user_id = :NEW.user_id)
           WHERE user_id IN 
             (SELECT user_id FROM employment_table WHERE employment_type = 'INT') 
             AND user_id IN 
               (SELECT user_id FROM company_table WHERE company_id = '10200');
  END CASE;
END absence_trigger;

它显然不起作用,我不知道该怎么做才能使它起作用。有什么建议么?

4

2 回答 2

4

将插入更改为:

insert into out (absence_reason, employee_id)
select :NEW.absence_reason, e.emp_no
  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';

这应该工作。请注意emp_no,您的示例结构employee_id中还有触发器插入。我假设emp_no是对的。也emp_typevs employment_type

最后在你的触发器中你有company_id引号。它真的是一个varchar2吗?如果可以,如果不是,请不要使用引号。

于 2013-01-07T08:07:37.847 回答
1

括号不平衡。价值的那一个没有关闭。这是您的特定错误的原因,但@DazzaL 的答案看起来是正确的解决方案。

于 2013-01-07T08:06:59.687 回答