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 8015193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:11:06+00:00 2026-06-04T20:11:06+00:00

I have the following trigger on my table, issue I am facing is in

  • 0

I have the following trigger on my table, issue I am facing is in my_hist table both old and new values are getting inserted when condition becomes updated = 'Y'.

How can I efficiently insert old values into my_hist table when condition becomes updated = 'Y'.

Thanks

CREATE OR REPLACE TRIGGER mytrig
    AFTER UPDATE
    ON mytab
    FOR EACH ROW
WHEN (
new.updated = 'Y'
      )
BEGIN
    INSERT INTO my_hist
      VALUES   (
                        :old.id,
                        :old.no,                        
                        :old.start_date,
                        :old.end_date,
                        SYSDATE
                  );
END mytrig;
/

Update 1

If I am updating updated = 'Y' then if I have records

id = 3, start_date=’01-Jan-2014′ and end_date=’31-Jan-2014′ and updated data
id = 3, start_date=='01-Jan-2014' and end_date='31-Mar-2014' then I will have two records in my_hist table are

id = 3 and start_date='01-Jan-2014' and end_date='31-Jan-2014'

and

id = 3 and start_date='01-Jan-2014' and end_date='31-Mar-2014'

Ideally I should have only id = 3 and start_date='01-Jan-2014' and end_date='31-Jan-2014' in my_hist table, not the second row with end_date='31-Mar-2014' because that is new data.

  • 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-04T20:11:09+00:00Added an answer on June 4, 2026 at 8:11 pm

    Something in your description is missing. If you do what you say you’re doing, your history table would have only one row and it would have the old data. If you also have a history row that has the new data, either your trigger is doing something other than what you posted or you have something else (i.e. another trigger) that is writing to the history table.

    Create the two tables

    SQL> ed
    Wrote file afiedt.buf
    
      1  create table my_tab (
      2    id number,
      3    start_date date,
      4    end_date date,
      5    no number,
      6    updated varchar2(1)
      7* )
    SQL> /
    
    Table created.
    
    SQL> ed
    Wrote file afiedt.buf
    
      1  create table my_hist (
      2    id number,
      3    start_date date,
      4    end_date date,
      5    no number,
      6    updated varchar2(1),
      7    update_date date
      8* )
    SQL> /
    

    Populate the initial data

    SQL> ed
    Wrote file afiedt.buf
    
      1  insert into my_tab
      2*   values( 3, date '2014-01-01', date '2014-01-31', 1, 'Y' )
    SQL> /
    
    1 row created.
    

    Create the trigger

    SQL> ed
    Wrote file afiedt.buf
    
      1  CREATE OR REPLACE TRIGGER mytrig
      2      AFTER UPDATE
      3      ON my_tab
      4      FOR EACH ROW
      5  WHEN (
      6  new.updated = 'Y'
      7        )
      8  BEGIN
      9      INSERT INTO my_hist( id,
     10                           no,
     11                           start_date,
     12                           end_date,
     13                           update_date )
     14        VALUES   (
     15                          :old.id,
     16                          :old.no,
     17                          :old.start_date,
     18                          :old.end_date,
     19                          SYSDATE
     20                    );
     21* END mytrig;
    SQL> /
    
    Trigger created.
    

    Now, when I update the row

    SQL> update my_tab
      2     set end_date = date '2014-03-31'
      3   where id = 3;
    
    1 row updated.
    

    there will be only one row in the MY_HIST table and that row will have the old values from the row in MY_TAB

    SQL> select * from my_hist;
    
            ID START_DAT END_DATE          NO U UPDATE_DA
    ---------- --------- --------- ---------- - ---------
             3 01-JAN-14 31-JAN-14          1   27-MAY-12
    

    If you see two rows, something else is writing the second row. My guess is that you have another trigger defined.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having the following issue: I have a trigger on a table A, whose
I have created the following trigger for insert in the deleted user table the
I have the following trigger on a table that redirects data and includes data
I have the following trigger on a blackhole table, that intercepts inserts and passes
I'm creating a trigger for a table, and I have the following statement: UPDATE
I have the following table and trigger but the trigger isn't setting the create_dt
I have the following trigger which gets fired when updated becomes Y . I
I have the following SQL I trigger in a C# app. All works well
I have created a Button with the following trigger: <Button Content=Test> <i:Interaction.Triggers> <i:EventTrigger EventName=Click>
I have the following function that is supposed to trigger anytime one of the

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.