I’m trying to create a trigger that will do the following.
After insert on Table A, query Table B based on an id (TableA.id=TableB.id) and insert corresponding info into TableA
I have a feeling I’m way off so far so any help would be appreciated
CREATE OR REPLACE TRIGGER myTrig
AFTER INSERT
ON TABLEA
BEGIN
INSERT INTO TABLEA
SELECT TABLEB.FIRST_NAME, TABLEB.LAST_NAME, SYSDATE
FROM TABLEA JOIN TABLEB ON
TABLEA.STUDENT_ID=TABLEB.STUDENT_ID
insert into TABLEA values (....);
END;
This will not work. Just think about what you are asking. You want a trigger that fires when a row is inserted into tableA to insert a row into tableA. When would the trigger stop firing?
Oracle is smart enough to step in and prevent the trigger spiralling into infinity:
Here’s what happens:
If by “update” you actually do mean UPDATE, and – crucially – depending on the precise logic you wish to implement then perhaps yes:
However this remains a bad idea. Triggers are hard to understand and can have a deleterious nimpact on the performance of our SQL. So I suggest you try to figure out a way of building your logic into the main body of your application, rather than trying to use a trigger.