Saturday, March 20, 2010

Oracle NO_DATA_FOUND exception for SELECT INTO

When a SELECT INTO does not return anything the NO_DATA_FOUND exception is raised. The exception is not raised if a aggregate function like SUM or AVG is used, but we cannot use that for a varchar column. So, I am now catching the NO_DATA_FOUND exception in my procedure.
 
SELECT INTO VARIABLE_NAME <SQL statement that raises NO_DATA_FOUND exception>
   EXCEPTION
     WHEN NO_DATA_FOUND THEN
             VARIABLE_NAME :=0; --DEFAULT value of VARIABLE_NAME
             DBMS_OUTPUT.PUT_LINE('');
     WHEN OTHERS THEN
      ROLLBACK;
      MESSAGE :=
            MESSAGE
         || 'SQLCODE: '
         || SQLCODE
         || ' SQLERRM:'
         || SUBSTR (SQLERRM, 1, 100);

      DBMS_OUTPUT.put_line ('ERROR: ' || MESSAGE);

No comments:

Post a Comment

Thank you for your feedback