PL/SQL block problem: No data found error

后端 未结 5 1982
眼角桃花
眼角桃花 2020-12-02 14:29
SET SERVEROUTPUT ON
DECLARE
    v_student_id NUMBER := &sv_student_id;
    v_section_id NUMBER := 89;
    v_final_grade NUMBER;
    v_letter_grade CHAR(1);
BEGIN         


        
相关标签:
5条回答
  • 2020-12-02 14:36

    There is an alternative approach I used when I couldn't rely on the EXCEPTION block at the bottom of my procedure. I had variables declared at the beginning:

    my_value VARCHAR := 'default';
    number_rows NUMBER := 0;
    .
    .
    .
    SELECT count(*) FROM TABLE INTO number_rows (etc.)
    
    IF number_rows > 0 -- Then obtain my_value with a query or constant, etc.
    END IF;
    
    0 讨论(0)
  • 2020-12-02 14:53

    This data not found causes because of some datatype we are using .

    like select empid into v_test

    above empid and v_test has to be number type , then only the data will be stored .

    So keep track of the data type , when getting this error , may be this will help

    0 讨论(0)
  • 2020-12-02 14:54

    When you are selecting INTO a variable and there are no records returned you should get a NO DATA FOUND error. I believe the correct way to write the above code would be to wrap the SELECT statement with it's own BEGIN/EXCEPTION/END block. Example:

    ...
    v_final_grade NUMBER;
    v_letter_grade CHAR(1);
    BEGIN
    
        BEGIN
        SELECT final_grade
          INTO v_final_grade
          FROM enrollment
         WHERE student_id = v_student_id
           AND section_id = v_section_id;
    
        EXCEPTION
          WHEN NO_DATA_FOUND THEN
            v_final_grade := NULL;
        END;
    
        CASE -- outer CASE
          WHEN v_final_grade IS NULL THEN
          ...
    
    0 讨论(0)
  • Might be worth checking online for the errata section for your book.

    There's an example of handling this exception here http://www.dba-oracle.com/sf_ora_01403_no_data_found.htm

    0 讨论(0)
  • 2020-12-02 15:02

    Your SELECT statement isn't finding the data you're looking for. That is, there is no record in the ENROLLMENT table with the given STUDENT_ID and SECTION_ID. You may want to try putting some DBMS_OUTPUT.PUT_LINE statements before you run the query, printing the values of v_student_id and v_section_id. They may not be containing what you expect them to contain.

    0 讨论(0)
提交回复
热议问题