2016-10-26 163 views
-1

我從3個月開始學習PLSQL。現在我正在練習顯式遊標。我已經寫了一個簡單的遊標,並且對於員工的查詢5行使用了屬性%ROWCOUNT,但我在屏幕上只收到了4名員工。爲什麼?爲什麼我沒有得到我獲取的確切行數?

SET SERVEROUTPUT ON 
DECLARE 
    CURSOR cur_emp IS 
    SELECT e.employee_id, e.last_name, d.department_id, d.department_name 
    FROM employees e 
    JOIN departments d 
    ON (e.department_id = d.department_id); 

    v_emp_id employees.employee_id%TYPE; 
    v_emp_name employees.last_name%TYPE; 
    v_dept_id departments.department_id%TYPE; 
    v_dept_name departments.department_name%TYPE; 
BEGIN 
    OPEN cur_emp; 
    LOOP 
     FETCH cur_emp 
     INTO v_emp_id, v_emp_name, v_dept_id, v_dept_name; 
     EXIT WHEN cur_emp%ROWCOUNT = 5; 
     DBMS_OUTPUT.PUT_LINE('EMPLOYEE ID: ' || v_emp_id || ' EMPLOYEE: ' || 
     v_emp_name || ' DEPARTMENT ID: ' || v_dept_id || ' DEPARTMENT NAME: ' || 
     v_dept_name); 
    END LOOP; 
    CLOSE cur_emp; 
END; 
/

image

回答

1

環路首先檢查條件,所以寫4名員工,檢查下一個的行數,並退出你必須把ROWCOUNT> 5,ROWCOUNT = 6或移動的環EXIT WHEN條件後您致電DBMS_OUTPUT

+0

謝謝您的幫助!我現在明白了。 – Rattlesnake

相關問題