I am not sure if I understood you correctly but this is what my understanding is from your question.
- You have one table:
Z (ATTRB, VAL)
- You want to create an insert trigger on it.
- The
trigger
should update the VAL
of already existing record in case ATTRB
of the existing record is same as ATTRB
of newly inserted record and VAL of the existing record is lower than newly inserted record's VAL
- The
trigger
should update the value of VAL
of the existing record to the VAL
of the newly inserted record.
Here is how you can do it using a trigger
.
- creating the table and adding one record:
SQL> CREATE TABLE Z (ATTRB VARCHAR2(1), VAL NUMBER);
Table created.
SQL> INSERT INTO Z VALUES ('A',1);
1 row created.
SQL> SELECT * FROM Z;
A VAL
- ----------
A 1
- Trigger code
SQL> CREATE TRIGGER Z_TRG BEFORE
2 INSERT ON Z
3 FOR EACH ROW
4 BEGIN
5 UPDATE Z
6 SET VAL = :NEW.VAL
7 WHERE ATTRB = :NEW.ATTRB
8 AND VAL < :NEW.VAL;
9 END Z_TRG;
10 /
Trigger created.
- Checking if the trigger is working by inserting other value
SQL> INSERT INTO Z VALUES ('A',2); -- adding higher value of VAL should update the existing record
1 row created.
SQL> SELECT * FROM Z;
A VAL
- ----------
A 2
A 2
SQL> INSERT INTO Z VALUES ('A',1); -- adding lower value of VAL will do nothing
1 row created.
SQL> SELECT * FROM Z;
A VAL
- ----------
A 2
A 2
A 1
SQL>
Cheers!!