I need to write a trigger on table t1 whenever so that when Iupdate or insert on that table, I also update a column on the same table with some conditions. I tried with my code, but I am getting mutating error.
Table t1 RESOURCEID NOT NULL VARCHAR2(40) DERIVEDTYPE NOT NULL VARCHAR2(40) LABEL NOT NULL VARCHAR2(80) ALTERNATIVELABEL VARCHAR2(80) DESCRIPTION NOT NULL VARCHAR2(256) CREATIONDATE DATE VERSION VARCHAR2(10) FORMAT NOT NULL VARCHAR2(20) AVAILABILITYDATE NOT NULL DATE ENDAVAILABILITYDATE NOT NULL DATE ENDARCHIVEDATE NOT NULL DATE ISSUEDDATE NOT NULL DATE RESOURCESTATE CHAR(1) LASTMODIFIEDDATE DATE PUBLISHER VARCHAR2(40) LANGUAGE NOT NULL VARCHAR2(256) HASRELATION CHAR(1) ADMINISTRATIVESTATE CHAR(1) OPERATIONALSTATE CHAR(1) USAGESTATE CHAR(1) MAXUNITS NUMBER ISPROTECTED CHAR(1)
I want to update the column value RESOURCESTATE before insert or update based on these conditions:
IF TRUNC(sysdate) >=TRUNC(rec.AvailabilityDate) and TRUNC(sysdate)
=TRUNC(rec.issueddate) and TRUNC(sysdate) =TRUNC(rec.ENDAVAILABILITYDATE) THEN update resources1 set resourcestate ='1'; END IF;
Can you help in writting a trigger for this?
The key point is NOT to do an update in the trigger on the same table, but to change the value of the :new BUFFER.
CODE is as follows: ==================== CREATE OR REPLACE TRIGGER TR_IU_RESOURCES BEFORE UPDATE or insert ON RESOURCES REFERENCING NEW AS NEW OLD AS OLD FOR EACH ROW Begin IF TRUNC(sysdate) >= TRUNC(:new.AvailabilityDate) and TRUNC(sysdate) < TRUNC(:new.endAvailabilityDate) THEN :NEW.resourcestate := '3'; END IF; IF TRUNC(sysdate) >= TRUNC(:New.issueddate) and TRUNC(sysdate) < TRUNC(:NEW.AvailabilityDate) THEN :NEW.resourcestate := '2'; END IF; IF TRUNC(sysdate) <= TRUNC(:NEW.ENDARCHIVEDATE) and TRUNC(sysdate) >= TRUNC(:NEW.ENDAVAILABILITYDATE) THEN :new.resourcestate := '1'; END IF; End TR_IU_RESOURCES;
For More Information
- What do you think about this answer? E-mail the editors at [email protected] with your feedback.
- The Best Oracle Web Links: tips, tutorials, scripts, and more.
- Have an Oracle or SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your technical Oracle and SQL questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.