Jaanna
Jaanna

Reputation: 1670

INSERT trigger with INSERT INTO WHERE condition

I am working on a trigger which needs INSERT INTO with WHERE logic.

I have three tables.

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

and finally I have the table out where data should be going only who have emp_type = Int in employment_table and have company_id = 10200

out:

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

Here is my trigger:

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;

It is obviously not working and I can't figure out what should I do to make it work. Any suggestions?

Upvotes: 0

Views: 1521

Answers (2)

DazzaL
DazzaL

Reputation: 21973

change the insert to this:

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

which should work. note you had emp_no in your sample structure yet employee_id in the trigger insert too. i've assumed emp_no is right. also emp_type vs employment_type.

Finally in your trigger you have company_id in quotes. Is it really a varchar2? if so OK, if not, don't use quotes.

Upvotes: 4

Peter Wooster
Peter Wooster

Reputation: 6089

The parentheses are not balanced. The one for values is not closed. This is the cause of your specific error, but @DazzaL's answer looks like the correct solution.

Upvotes: 1

Related Questions