In a recent interview I was asked to write a simple PL/SQL code to throw NO_DATA_FOUND exception without actually raising it from the code. I am wondering what could be a simple pl/sql snippet to do so. Probably using dual?
3 Answers 3
Many ways, but as you suggested, you can use DUAL
:
CREATE PROCEDURE NODATAFOUND
AS
nowt VARCHAR(10);
BEGIN
SELECT * INTO nowt FROM DUAL WHERE 1=0;
END;
/
It'll produce a ORA-01403: no data found
when execute
d.
I would use an existing table from project structure with an impossible where clause. For example a negative number on PK when PK i defined as positive, etc.
Strange question in an interview....I never got that kind.
-
The idea behind the question was to use simplistic code without assuming any tables or raising this exception I think. But yes this would obviously work.Aniket Thakur– Aniket Thakur2016年06月04日 21:18:14 +00:00Commented Jun 4, 2016 at 21:18
How about
raise no_data_found;