22

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

Srini V
  • 11,045
  • 14
  • 66
  • 89
JGS
  • 813
  • 2
  • 8
  • 17
  • 2
    I get `5` for both blocks in Oracle 11g. Unfortunately I dont have 9i! – Maheswaran Ravisankar Apr 02 '14 at 15:11
  • Maybe something is different with 9.x, because on 11g, it works as expected. – OldProgrammer Apr 02 '14 at 15:12
  • Fascinating! Does it do the same thing if you use `select rownum from dual connect by level <=5` instead? – Jeffrey Kemp Apr 03 '14 at 05:35
  • Using rownum does not change the behaviuor still returns 1... this is really weird. I'd like to know if someone has tried this in another 9.2i database just to check if this is the expected return... – JGS Apr 03 '14 at 09:39
  • 8
    Sounds like an optimizer Bug.... can you try to run the PL/SQL without any optimizing? I guess the optimizer sees the select from dual, infers a select from dual will only return one row and strips the loop to a single call... just a wild guess ;-) – Falco Apr 09 '14 at 14:24
  • 1
    I get 5 for both blocks. I am on Oracle 10G. If I try this on 9.2.0.7.0, I am seeing the same results as you. – abhi Apr 09 '14 at 14:48
  • 2
    It's a bug. Premier support for 9.2 expired in 2007 It's fixed in later versions. You should really think about upgrading your server - we're on 11gR2 and I thought we were behind the times...Sorry I think you need to be logged into Oracle support to see the link I posted below – kayakpim May 01 '14 at 11:46
  • https://support.oracle.com/epmos/faces/DocumentDisplay?_afrLoop=1045574615030914&id=189908.1&_afrWindowMode=0&_adf.ctrl-state=119ehzezge_166 – kayakpim May 01 '14 at 11:46
  • 2
    See this thread on asktom.oracle.com. https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:40476301944675#40490066762235 Tom Kyte explains on other threads that there are optimizer diffs between 9i and 10g. – Lord Peter May 01 '14 at 12:20
  • [This](http://stackoverflow.com/q/22032065/266304) looks like the same issue as Tom's article too. I've check that and the PL/SQL version occur in 9.2.08 and not in later versions. – Alex Poole May 01 '14 at 15:47

1 Answers1

6

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.

Community
  • 1
  • 1
Jon Heller
  • 34,999
  • 6
  • 74
  • 132
  • Can you add bug number? That it is a bug is rather obvious based upon preceeding comments. – Guido Leenders May 04 '14 at 17:56
  • 6
    @GuidoLeenders I looked but could not find the bug number. I added this answer to summarize the comments, in case no better answer is provided. I marked it as community wiki so I won't get any points for repeating what everyone else is saying. – Jon Heller May 05 '14 at 01:38
  • @jonearles: Then don't post it as an answer. This should be a comment instead. – Patrick Hofman May 05 '14 at 06:48
  • 7
    If you don't like this answer then please **EDIT** it. That's why it's a community wiki. All the comments are basically saying "it's a bug". This question deserves an answer, even if it's not a very good one. Nobody wants to read through all these comments. I got this idea from [this thread](http://meta.stackoverflow.com/q/251597/409172). – Jon Heller May 05 '14 at 13:11