Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2003
    Posts
    35

    Question Unanswered: Nesting if/else if statements in a while loop

    Can I some how nest an if/else if ladder in a while loop?
    I am trying to do so right now, but am getting some errors.

    If it will help here is a snipet of my code/logic.
    east_max1, east_max2, east_max3 are defined as eastRows%rowtype
    is there a problem using them in the if condition if there is no values in them?

    begin
    open eastRows;
    loop
    exit when not eastRows%found;
    fetch eastRows into east_var;
    if east_max1.easttsales < east_var.easttsales
    then
    east_max3 := east_max2;
    east_max2 := east_max1;
    east_max1 := east_var;
    else if east_max2.easttsales < east_var.easttsales
    then
    east_max3 := east_max2;
    east_max2 := east_var;
    else if east_max3.easttsales < east_var.easttsales
    then
    east_max3 := east_var;
    else
    east_var := east_var;
    end if;


    end loop;
    close eastRows;

    ANy help would be great.

    Also as a bonus questoin, how can I get around the problem using
    Rank() over... in PLSQL when defining a cursor, I get an error about using RANK() in order by statement.

    THanks Again.
    "My Idea is that everyone should be required to use small fonts. That way we'll save disk space.
    ~Asok.

  2. #2
    Join Date
    Mar 2004
    Posts
    2
    You need an end if for every if statement.

    Otherwise use elsif

  3. #3
    Join Date
    Mar 2004
    Posts
    23

    Re: Nesting if/else if statements in a while loop

    begin
    open eastRows;
    loop

    exit when not eastRows%found;
    fetch eastRows into east_var;

    -- note usually put exit when test after fetch, not before
    -- e.g. exit when eastRows%notfound;

    -- see below use elsif

    if east_max1.easttsales < east_var.easttsales
    then
    east_max3 := east_max2;
    east_max2 := east_max1;
    east_max1 := east_var;
    elsif east_max2.easttsales < east_var.easttsales
    then
    east_max3 := east_max2;
    east_max2 := east_var;
    elsif east_max3.easttsales < east_var.easttsales
    then
    east_max3 := east_var;
    else
    east_var := east_var;
    end if;


    end loop;
    close eastRows;

    ANy help would be great.

    Also as a bonus questoin, how can I get around the problem using
    Rank() over... in PLSQL when defining a cursor, I get an error about using RANK() in order by statement.

    THanks Again. [/SIZE][/QUOTE]

  4. #4
    Join Date
    May 2003
    Location
    Universe.MilkyWay.SolarSystem.Earth.NorthAmerica.Canada.Quebec.Chicoutimi;
    Posts
    9

    Cool

    Here is how I would do your code :

    BEGIN
    FOR east_var IN eastRows LOOP
    IF east_max1.easttsales < east_var.easttsales THEN
    east_max3 := east_max2;
    east_max2 := east_max1;
    east_max1 := east_var;
    ELSIF east_max2.easttsales < east_var.easttsales THEN
    east_max3 := east_max2;
    east_max2 := east_var;
    ELSIF east_max3.easttsales < east_var.easttsales THEN
    east_max3 := east_var;
    ELSE
    east_var := east_var;
    END IF;
    END LOOP;
    END;

    Note that I use a different way of handling the cursor (FOR ... IN ... LOOP), it is similar to C# and VB (foreach). I find that way more convenient for debugging and understanding code.

    Hope that helps !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •