【问题标题】:PLSQL: ORA- 00904 err "Invalid identifier" & PLS- 00487 error as Invalid Identifier & Invalid reference to variable 'i'PLSQL: ORA-00904 err "Invalid identifier" & PLS-00487 error as Invalid Identifier & Invalid reference to variable 'i'
【发布时间】:2020-03-31 07:52:34
【问题描述】:

在 PLSQL 块语句中,将执行 DML 操作。我将 FORALL 与 BULK COLLECT 一起使用。下面提到的PLSQL语句-

declare
 v_sub  tab_a%rowtype;
 v_res  varchar2(50);
 type v_rec_tbl is table of tab_out%rowtype;
 v_rec v_rec_tbl;
 cursor C is select b.sid, a.sin, 'N', SYSDATE from tab_a a, tab_b b;
begin
 open C;
 fetch C bulk collect into v_rec limit 1000;
 for i in (select a.sin from tab_a a, tab_b b where b.sid = .....)
  loop
  select * into v_sub from tab_a where sin = i.sin;
  end loop;

 FORALL i in v_rec.FIRST..v_rec.LAST
  insert into tab_out
  select b.sid, i.sin, 'N', SYSDATE from tab_a a, tab_b b where b.sid = ......;
 commit;
close C;
end;
/

当我执行上面的 PL/SQL 语句时,在i.sin 中的insert into tab_out 行出现错误ORA-00904PLS-00487Invalid IdentifierInvalid reference to variable 'i'。 我该如何解决这个错误,以便快速插入记录。

【问题讨论】:

  • 如果你想让它更快,不要使用慢游标、循环或 PL/SQL
  • @a_horse_with_no_name 是的,我想让它更快,但在FOR LOOP 中让它变慢。有没有其他方法可以使这个 PLSQL 块语句更快。
  • 然后彻底摆脱 PL/SQL 和 FOR 循环

标签: sql oracle plsql bulkinsert


【解决方案1】:

v_rec(i).sin,不是i.sin

不过,您是说您想让它运行得更快。你为什么选择让它变慢的方法?您使用光标,循环,...诸如此类。

直接插入行,不涉及PL/SQL(除非必要):

insert into tab_out (sid, sin, ...)
  select b.sid, i.sin, ...
  from tab_a a join tab_b b on ...
  where ... ;

【讨论】:

  • 感谢您给我答案。在线for i in (select ...) 面临性能问题。有没有其他方法可以提高查询性能。
  • 不客气。至于性能,我已经告诉过你我的想法。如果可能,请使用单个 INSERT 语句,而不使用任何 PL/SQL。
  • 我在没有 PLSQL 的情况下使用但需要时间。我想在FOR i IN (select ...) 中调整代码,因为这需要更多时间。
  • 循环通常比纯 SQL 慢。它逐行处理数据,这是缓慢的。如果我是你,我宁愿花一些时间调整 SQL 本身。确保您在 WHERE 子句中使用的列已编入索引。收集所有相关表的统计信息。检查解释计划。
【解决方案2】:

您必须使用集合上的索引v_rec,如下所示:

FORALL idx in 1..v_rec.COUNT -- changes here
  insert into tab_out
  select b.sid, v_rec(idx).sin, 'N', SYSDATE from tab_a a, tab_b b where b.sid = ......;
   -- see the usage of the idx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多