你exit when c_Students%NOTFOUND;
放在下面所以最后一次为没有数据时也会执行
DBMS_OUTPUT.PUT_LINE(v_firstName|| ' '||v_lastName);
所以就变成多一条数据

解决方案 »

  1.   

    fetch c_Students INTO v_firstName,v_lastName;
    exit when c_Students%NOTFOUND;
    DBMS_OUTPUT.PUT_LINE(v_firstName|| ' '||v_lastName);
    即可
      

  2.   

    create or replace procedure PrintStudents(
    p_Major In students.major%TYPE) as
    v_firstName varchar2(20);
    v_lastName varchar2(20);
    cursor c_students is
    select first_name,last_name from students where major=p_Major;
    BEGIN
    open c_Students;
             fetch c_Students INTO v_firstName,v_lastName; 
    loop
                 exit when c_Students%NOTFOUND;

        DBMS_OUTPUT.PUT_LINE(v_firstName|| ' '||v_lastName);

                 fetch c_Students INTO v_firstName,v_lastName;
    END LOOP;
    close c_Students;
    END;
      

  3.   

    ........
    BEGIN
        open c_Students;
        loop
            fetch c_Students INTO v_firstName,v_lastName;
            exit when c_Students%NOTFOUND; <---fetch的时候发现没有了就退出.          
            DBMS_OUTPUT.PUT_LINE(v_firstName|| ' '||v_lastName);
        END LOOP;
        close c_Students;
    END;
      

  4.   

    loop
    fetch c_Students INTO v_firstName,v_lastName;
             exit when c_Students%NOTFOUND;
    DBMS_OUTPUT.PUT_LINE(v_firstName|| ' '||v_lastName);

    END LOOP;判断notfound应该在输出的前面。如果没有数据将不进行输出。