3 Replies - 1048 Views - Last Post: 17 February 2012 - 06:24 AM Rate Topic: -----

#1 asim_ishaq  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-March 10

Oracle Plsql Hell

Posted 16 February 2012 - 11:49 PM

declare
  v_counter number := 1000;
begin
  for v_counter in 1 .. 10 loop
    dbms_output.put_line('In loop v_counter = ' || to_char(v_counter));
  end loop;

  dbms_output.put_line('After Loop v_counter = ' || to_char(v_counter));

end;
/



What is the value of v_counter after for loop? Let me guess its 10. The strange thing is that within for loop the value of v_counter does change from 1 to 10 but after for loop it reset to 1000. I have found another way of writing this code that works with oracle.

If we use a while loop and increment value of v_counter within loop then it actually preserve the change. look at the following code:

declare
  v_counter number := 1000;
begin
  v_counter := 0;
  
  while v_counter < 10 loop
  
    dbms_output.put_line('In loop v_counter = ' || to_char(v_counter));
    v_counter := v_counter + 1;
    
  end loop;

  dbms_output.put_line('After Loop v_counter = ' || to_char(v_counter));

end;
/



Is This A Good Question/Topic? 0
  • +

Replies To: Oracle Plsql Hell

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3402
  • View blog
  • Posts: 9,619
  • Joined: 08-June 10

Re: Oracle Plsql Hell

Posted 17 February 2012 - 05:17 AM

I'd hazard a guess at a scope problem. some PLs open a new scope in loops.
Was This Post Helpful? 0
  • +
  • -

#3 asim_ishaq  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-March 10

Re: Oracle Plsql Hell

Posted 17 February 2012 - 05:55 AM

View PostDormilich, on 17 February 2012 - 05:17 AM, said:

I'd hazard a guess at a scope problem. some PLs open a new scope in loops.


If for loop creates a new scope then why not the while loop?
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Oracle Plsql Hell

Posted 17 February 2012 - 06:24 AM

Mostly like it something to do with the scoping rules. Most Local Wins
If it behaves like a Functional Language, then those are different "variables" with the same name.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1