Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8136759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:51:38+00:00 2026-06-06T10:51:38+00:00

I am quite new to triggers so obviously I am doing something wrong somewhere.

  • 0

I am quite new to triggers so obviously I am doing something wrong somewhere. I am working on a report table which will get the data from original tables. For the sake of simplicity, let’s say that there is one table and then there is one reporting table.

Original table (orig_tab)

CREATE TABLE orig_tab (
PK     NUMBER(8)       not null,
NAME   VARCHAR2(20)            ,
);

INSERT INTO orig_tab (PK, NAME) VALUES (1, 'AAA');
INSERT INTO orig_tab (PK, NAME) VALUES (2, 'BBB');
INSERT INTO orig_tab (PK, NAME) VALUES (3, 'CCC');

Then there is reporting table (rep_tab)

CREATE TABLE rep_tab (
PK     NUMBER(8)       not null,
NAME   VARCHAR2(20)            ,
);

Now from user inteface, someone changes the value of record 2. Obviously, this should be treated as an insert (because this record doesn’t exist) for reporting table. Then after sometime, the value is changed so it is an update case for reporting table.

Question: How may I make this kind of trigger? I assume that it is a merge statemement case.

This is what I have done:

create or replace trigger vr_reporting_trigger
after update on orig_tab
  for each row
begin
  MERGE INTO rep_tab d
  USING (SELECT pk FROM orig_tab) s
  ON (d.pk = s.pk)
  WHEN MATCHED THEN
  UPDATE SET d.pk = s.pk,
             d.name = s.name
  WHEN NOT MATCHED THEN
  INSERT (d.pk, d.name) VALUES (s.pk, s.name);
end vr_reporting_trigger;

Any suggestions or recommendations that can help me to figure it out? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:51:40+00:00Added an answer on June 6, 2026 at 10:51 am

    There are some corner cases that aren’t handled in previous answers.

    What if a matching pk already exists in the reporting table, when a row is inserted. (We wouldn’t normally expect this to happen, but consider what would happen if someone deleted a row from the orig_tab, and then inserted it again. (This is the kind of problem that’s going to crop up in production, not in test, at the most inopportune time. Better to plan for it now.)

    BEGIN
       IF inserting THEN
          -- insure we avoid duplicate key exception with a NOT EXISTS predicate
          INSERT INTO rep_tab(pk,name)
          SELECT :new.pk, :new.name FROM DUAL
          WHERE NOT EXISTS (SELECT 1 FROM rep_tab WHERE pk = :new.pk);
          -- if row already existed, there's a possibility that name does not match
          UPDATE rep_tab t SET t.name = :new.name 
           WHERE t.pk = :new.pk;
          -- could improve efficiency of update by checking if update is actually
          -- needed using a nullsafe comparison ( t.name <=> :new.name );
       ELSIF updating THEN
          -- handle updates to pk value (note: the row to be updated may not exist
          -- so we need to fallthru to the merge)
          IF :new.pk <> :old.pk THEN
             UPDATE rep_tab t
                SET t.pk = :new.pk
                  , t.name = :new.name
              WHERE t.pk = :old.pk ;
          END IF;
          MERGE INTO rep_tab d
          USING DUAL ON (d.pk = :old.pk)
          WHEN MATCHED THEN
          UPDATE SET d.name = :new.name
          WHEN NOT MATCHED THEN
          INSERT (d.pk,d.name) VALUES (:new.pk,:new.name);
       END IF;
    END;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quite new to maven here so let me explain first what I am trying
Still quite new to Haskell.. I want to read the contents of a file,
Iam quite new to functions in SQL and I would like to create a
I'm quite new to php and have been reading Larry Ullman book to develop
I'm quite new to c++, but I've got the hang of the fundamentals. I've
I'm quite new to OpenId and I'm having a bit of a problem understanding
I'm quite new to this (understanding the WP Guts), and I wanted to understand
I'm quite new to jQuery, and I've written a simple function to check the
I am quite new with Linq to SQL and MVC but I follow the
I'm quite new to LINQ and was wondering what was the best design for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.