Thursday, March 14, 2013

Handling No_data_found errors

There are multiple ways provided but the simplest and cleanest was this... enclose the select that is potential for this error like you are fetching and not guaranteed to fetch the one row and assigning to a local variable in a BEGIN..END block.

http://stackoverflow.com/questions/3891829/pl-sql-is-there-a-nvl-for-empty-rows


Then introduce within the begin..end the exception block, handle it the way you like to proceed and carry on with the business. This helps in scenarios where a failed fetch is no showstopper for the code to carry on with execution.

DECLARE 
  test VARCHAR2(255):=0; 
BEGIN 
  BEGIN
    SELECT id INTO test FROM sku WHERE id = 1515;
  EXCEPTION
    WHEN NO_DATA_FOUND THEN
      test := '0';
  END;

  dbms_output.put_line('Result:' || test); 
END;

No comments:

Post a Comment