Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Trigger to insert and update

zephyr223Jun 21 2018 — edited Jun 21 2018

Good Morning All,

Got a dilemma I'm trying to figure out and hope someone can provide some guidance.  I have a trigger created and works just fine that where after a record is inserted/updated/deleted into a table named Attachment, it inserts that a change was made to that attachment table in another table called CHANGES. So for example, here is the structure of Attachment and a sample record:

ATTACHMENT

AttachIDReport#AttDescriptionAttachmentWhoAttachedAttachmentType
11258Tattoo(this is an actual attachement)4343Jpeg

If an Insert was just performed in ATTACHMENT, here is what is inserted in the CHANGES table based on a trigger:

CHANGES

IDTABLENAMEReport#ActionNameDateInsertedVersionNumber
1ATTACHMENT1258INS14-jun-2018

Here is the trigger and this works:

create or replace TRIGGER  "ATTACHMENTS_AFTER_INS_UPD_TRG"

AFTER INSERT OR UPDATE

    ON ATTACHMENT

    FOR EACH ROW

DECLARE

  l_dml_type varchar2(10);

BEGIN

IF (inserting) then

  l_dml_type := 'INS';

END IF;

IF (updating) then

  l_dml_type := 'UPD';

END IF;

INSERT INTO CHANGES

   ( ID,

   TABLENAME,

     REPORT#,

     ACTIONNAME,

     dateinserted)

   VALUES

     (change_ID_SEQ.NEXTVAL,

     'ATTACHMENTS',

     :new.report#,

     l_dml_type,

     sysdate);

END;

My issue is, I now have a requirement to insert a column named VERSIONNUMBER into CHANGes table.  This versionnumber is located in another table which is the parent table to attachment.  The name of the parent table is called REPORT and it is related to attachment by report# column.  So 1 report, can have multiple attachments.  So, now whenever a change is done in attachment I have to also get the current versionnumber from the report table (related by report#) into the changes table.  VersionNumber can change per report depending on other actions to the report that I have not listed here.  So here is what I expect to see now if I updated the same attachement/report# 3 times.

Day 1, insert and the versionnumber from report table was 1 at that time.

Day 2, updated same attachement and versionnumber was still 1 in report table

Day 3, udpated same attachement but now versionnumber has changed to 2 in report table so have to record it as 2 in CHANGES table. 

CHANGES

IDTABLENAMEReport#ActionNameDateInsertedVersionNumber
1ATTACHMENT1258INS14-jun-20181
2ATTACHMENT1258UPD15-jun-20181
3ATTACHMENT1258UPD16-jun-20182

I hope i explained the situation as clear as I can.  I guess my problem is joining 2 tables to achieve what I am tasked to do.  If there is a better way to do this, please let me know.  Thanks in advance for any help.

This post has been answered by Mustafa KALAYCI on Jun 21 2018
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 19 2018
Added on Jun 21 2018
10 comments
569 views