I ran into this problem while developing a trigger in Oracle: ORA-01403: no data found . I did some research and realized the root of the problem. However, error exception handling prevents the above error, but does not solve my problem.
I am currently looking for the best solution to fulfill fewer requests / maximize performance. I will try to describe a scenario that creates simple examples for a real structure.
Scenario
I have a "date reference" table to set time periods, say:
CREATE TABLE DATE_REFERENCE (
DATE_START DATE NOT NULL,
DATE_END DATE NOT NULL,
CONSTRAINT PK_DATE_REFERENCE PRIMARY KEY(DATE_START, DATE_END)
);
When the trigger fires, I will have one field DATE- let's say DATE_GIVEN(for example, sake). What I need:
- Find a line
DATE_REFERENCEin which DATE_GIVEN BETWEEN DATE_START AND DATE_END(easy); OR - If the previous option does not return data, I need to find the next closest
DATE_STARTto DATE_GIVEN.
In both cases, I need to get a row with all the columns from the table DATE_REFERENCE, regardless of whether it matches options 1 or 2. It was there that I encountered the described problem.
I wrote this test block for testing and tried to find a solution. The example below does not work , I know; but that’s exactly what I want to accomplish (in concept). I added comments as -- Lots of codeto clarify what would be part of a more complex trigger:
DECLARE
DATE_GIVEN DATE;
RESULTROW DATE_REFERENCE%ROWTYPE;
BEGIN
DATE_GIVEN := TO_DATE('2014-02-26 12:30:00', 'YYYY-MM-DD HH24:MI:SS');
SELECT
* INTO RESULTROW
FROM
DATE_REFERENCE
WHERE
DATE_GIVEN BETWEEN DATE_START AND DATE_END;
IF RESULTROW IS NULL THEN
SELECT
* INTO RESULTROW
FROM
DATE_REFERENCE
WHERE
DATE_START > DATE_GIVEN
AND ROWNUM = 1
ORDER BY DATE_START ASC;
END IF;
END;
, PL/SQL , , RESULTROW , , ?
, , . /!