I have the table named EPOCA with this schema:
CREATE TABLE EPOCA
(
ID INT
CONSTRAINT PK_EPOCA PRIMARY KEY,
NOME VARCHAR(250),
DATE_INITIAL DATE
CONSTRAINT NN_EPOCA_DATA_INI NOT NULL,
DATE_END DATE,
CONSTRAINT CK_EPOCA_DATAS CHECK (DATE_INITIAL < DATE_END)
);
And even though I already check if the initial date is smaller then the end date, I need to check that when I insert a new EPOCA the dates I insert will not overlap with any of the present dates.
I developed this trigger:
CREATE OR REPLACE TRIGGER TRGEPOCASNAOSOBREPOSTAS
BEFORE INSERT OR UPDATE
ON EPOCA
FOR EACH ROW
DECLARE
FLAG_DATE NUMBER;
BEGIN
FLAG_DATE := 0;
IF INSERTING THEN
SELECT E.ID INTO FLAG_DATE FROM EPOCA E WHERE E.DATE_INITIAL < :NEW.DATE_INITIAL AND E.DATE_END > :NEW.DATE_END;
IF FLAG_DATE <> 0 THEN
RAISE_APPLICATION_ERROR(-2098, 'INSERT FAILED BECAUSE SELECTED DATES OVERLAP EXISTENT ONES');
END IF;
ELSIF UPDATING THEN
SELECT E.ID INTO FLAG_DATE FROM EPOCA E WHERE E.DATE_INITIAL < :NEW.DATE_INITIAL AND E.DATA_END > :NEW.DATA_END;
IF FLAG_DATE <> 0 THEN
RAISE_APPLICATION_ERROR(-2099, 'UPDATE FAILED BECAUSE SELECTED DATES OVERLAP EXISTENT ONES');
END IF;
END IF;
END;
The error I am getting when inserting is because no data was found on line 7 of the trigger and when I am updating the table the error is because the table is in mutation and the trigger cannot read the table, which makes sense but I don't know how to solve it.
Any suggestions on how to solve this problem?