PL / SQL将行存储到变量中并在for循环中使用它们

时间:2014-10-26 14:46:22

标签: database oracle loops plsql cursor

我正在编写脚本并在将行值保存到变量时出错 我正在尝试更新我需要从其他相应表中获取的表

DECLARE
cur SYS_REFCURSOR;
dept_row department%rowtype;
employee_row employee%rowtype;
status number :=0;
BEGIN

FOR employee_details IN (select * from employee_details_table)
LOOP
select * into dept_row from department dept where employee_details.dept_id = dept.dept_id;
select * into employee_row from employee emp where emp.dept_id = empoyee_details.dept_id;

IF employee_details.valid <> employee_row.valid THEN
 // call SP which uses values from both dept_row & employee_row
END IF;
END LOOP;
COMMIT;
END;
/

当我运行上面的SQL时,我收到错误。 ORA-01722 ORA-06512

我无法弄清楚上面的代码有什么问题。 请帮忙

由于

1 个答案:

答案 0 :(得分:0)

您错误地使用了collection。另外,最好避免loop使用for并使用bulk collectforall statement来最小化context switch之间sql and plsql engines的开销。

DECLARE
  TYPE t_bulk_collect_test_tab IS TABLE OF employee_table%ROWTYPE;

  l_tab t_bulk_collect_test_tab;

  CURSOR c_data IS
    SELECT *
    FROM employee_table;
BEGIN
  OPEN c_data;
  LOOP
    FETCH c_data
    BULK COLLECT INTO l_tab LIMIT 10000;
    EXIT WHEN l_tab.count = 0;

.......

End;

有关集合,批量收集和forall语句的更多示例,请参阅此处的示例http://oracle-base.com/articles/9i/bulk-binds-and-record-processing-9i.php

请记住,最好避免loopsRow by rowslow by slow