【问题标题】:INSERT trigger with INSERT INTO WHERE condition带有 INSERT INTO WHERE 条件的 INSERT 触发器
【发布时间】:2012-12-20 22:37:52
【问题描述】:

我正在开发一个需要 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 在employment_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;

它显然不工作,我不知道我应该怎么做才能让它工作。有什么建议吗?

【问题讨论】:

  • 为什么它“显然”不起作用?错误信息是什么?
  • ORA-00933。 SQL 命令未正确结束。
  • 为什么要在触发器中计算而不是仅仅构建视图?

标签: oracle plsql oracle11g plsqldeveloper


【解决方案1】:

将插入更改为:

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_typeemployment_type

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

【讨论】:

  • 感谢您的帮助。这当然是我需要的:-)
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2021-11-16
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    相关资源
    最近更新 更多