在Oracle數據庫中,游標是一種用于處理和操作查詢結果集的對象
DECLARE
CURSOR c_employees IS
SELECT employee_id, salary
FROM employees
WHERE department_id = 10;
OPEN c_employees;
LOOP
FETCH c_employees INTO v_employee_id, v_salary;
EXIT WHEN c_employees%NOTFOUND;
-- Perform batch operations here, e.g., update salary
UPDATE employees
SET salary = salary * 1.1
WHERE employee_id = v_employee_id;
END LOOP;
CLOSE c_employees;
COMMIT;
將上述代碼片段組合在一起,形成一個完整的PL/SQL匿名塊,用于在批量操作中應用游標:
DECLARE
CURSOR c_employees IS
SELECT employee_id, salary
FROM employees
WHERE department_id = 10;
v_employee_id employees.employee_id%TYPE;
v_salary employees.salary%TYPE;
BEGIN
OPEN c_employees;
LOOP
FETCH c_employees INTO v_employee_id, v_salary;
EXIT WHEN c_employees%NOTFOUND;
-- Perform batch operations here, e.g., update salary
UPDATE employees
SET salary = salary * 1.1
WHERE employee_id = v_employee_id;
END LOOP;
CLOSE c_employees;
COMMIT;
END;
/
這個示例展示了如何在批量操作中使用游標來更新特定部門員工的薪水。你可以根據實際需求修改查詢條件和操作類型。