Simple PL/SQL code to throw NO_DATA_FOUND exception

oracleplsql

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?

Best Answer

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 executed.