Oracle游標是一種用于存儲查詢結果集的數據結構
DECLARE
CURSOR c_employees IS
SELECT * FROM employees;
BEGIN
OPEN c_employees;
END;
DECLARE
CURSOR c_employees IS
SELECT * FROM employees;
v_employee_id employees.employee_id%TYPE;
v_first_name employees.first_name%TYPE;
BEGIN
OPEN c_employees;
FETCH c_employees INTO v_employee_id, v_first_name;
CLOSE c_employees;
END;
BEGIN
CLOSE c_employees;
END;
DECLARE
CURSOR c_employees IS
SELECT * FROM employees;
v_employee_id employees.employee_id%TYPE;
v_first_name employees.first_name%TYPE;
BEGIN
OPEN c_employees;
FETCH c_employees INTO v_employee_id, v_first_name;
CLOSE c_employees;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found in the cursor.');
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Too many rows returned by the cursor.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
在這個示例中,我們使用了EXCEPTION子句來捕獲和處理可能發生的錯誤,如NO_DATA_FOUND(游標中沒有數據)、TOO_MANY_ROWS(游標返回了太多行)以及其他未知錯誤。當發生這些錯誤時,我們使用DBMS_OUTPUT.PUT_LINE輸出相應的錯誤信息。