Why does a connect by expression in a FOR loop, execute only once?

断了今生、忘了曾经 提交于 2019-12-09 07:51:28

问题


I just found what I think is somewhat unexpected behavior in PLSQL vs SQL in Oracle.

If I run this query on SQLDeveloper I get 5 results:

select level lvl from dual connect by level <=5;

But if i run this statement in SQLDeveloper:

declare
  w_counter number :=0;
begin
  for REC in (select level lvl from dual connect by level <=5)
  loop
    w_counter := w_counter+1;
  end loop;
  dbms_output.put_line('W_COUNTER: '|| w_counter);
end;

The variable w_counter finishes with value 1 (weird)

but the weirdest part is that if I encapsulate the query in a subquery... something like:

declare
  w_counter number :=0;
begin
  for REC in (select * from (select level lvl from dual connect by level <=5))
  loop
    w_counter := w_counter+1;
  end loop;
  dbms_output.put_line('W_COUNTER: '|| w_counter);
end;

The w_counter variable finishes with value 5...

What do you have to say to this?

I am using Oracle 9.2i


回答1:


It's a bug in a version of Oracle 9i confirmed up to 9.2.0.8, but not beyond.

It was previously discussed on Ask Tom, the response simply being that "sqlplus does that".

Premier support for Oracle 9.2 ended on 2007-07-31 and extended support ended on 2010-06-30. To fix this it is recommended that you upgrade to a current version of Oracle; if unable, you should patch your database past version 9.2.0.8.



来源:https://stackoverflow.com/questions/22815549/why-does-a-connect-by-expression-in-a-for-loop-execute-only-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!