Results 1 to 4 of 4

Thread: for loops incrementing in pl/sql

  1. #1
    Junior Member
    Join Date
    Oct 2008
    Answers
    1

    Red face for loops incrementing in pl/sql

    how we increment our for loops by 2
    we can only increment by 1 which is default one.
    for eg- For i in 1..4 loop

    and also i cant be the target of assignment,
    so what if i want to increment by 2 like i+2?
    is there any solution for this..


  2. #2
    Expert Member
    Join Date
    Sep 2007
    Answers
    697

    Re: for loops incrementing in pl/sql

    SQL> begin
    2 for i in 1 .. 100 loop
    3 i := i + 4;
    4 end loop;
    5 end;
    6 /
    i := i + 4;
    *
    ERROR at line 3:
    ORA-06550: line 3, column 5:
    PLS-00363: expression 'I' cannot be used as an assignment target
    ORA-06550: line 3, column 5:
    PL/SQL: Statement ignored

    You can use the loop index inside the loop, but you can not change it. If you want to loop by an increment other than one, you will have to do so programmatically as the FOR loop will only increment the index by one.

    In such cases it is better to use while.


  3. #3
    Moderator
    Join Date
    Jun 2007
    Answers
    2,074

    Re: for loops incrementing in pl/sql

    For always increments the value by 1 only.

    if you want to do something else on increment by 2 then check the value of i. If that is divided by 2 do what ever you want.


  4. #4
    Expert Member
    Join Date
    Nov 2008
    Answers
    300

    Re: for loops incrementing in pl/sql

    You can use the following syntax:

    Declare
    i number :=0;
    LOOP
    i:= i + 2;
    'do your coding here';
    exit when i:=100;
    END LOOP;


  5.  Sponsored Ads
    Ad


     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact