Question

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.

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top