1. Get rid of all advertisements and get unlimited access to documents by upgrading to Premium Membership. Upgrade to Premium Now and also get a Premium Badge!

Loop Instruction

Discussion in 'SQL PL/SQL' started by tomloera, Feb 28, 2011.

  1. tomloera

    tomloera Forum Expert

    Messages:
    167
    Likes Received:
    6
    Trophy Points:
    260
    Location:
    Monterrey Mexico
    Hi friends;

    Is posible use the loop instruction in a SQL program?

    If the answered is yes can give me an example?

    Thanks and advance

    Tom
     
  2. zargon

    zargon Community Moderator Forum Guru

    Messages:
    2,347
    Likes Received:
    348
    Trophy Points:
    1,430
    Location:
    Aurora, CO
    LOOP is a PL/SQL instruction; an example follows:

    Code (SQL):
     
    DECLARE
     mydt DATE;
     olddt varchar2(10);
    BEGIN
     FOR i IN 2000..2016 loop
      olddt:= '02/04/'||i;
      SELECT last_day(to_date(olddt, 'MM/DD/RRRR'))
      INTO mydt
      FROM dual;
      dbms_output.put_line(olddt||'   '||mydt);
     END loop;
    END;
    /
     
    This is called a FOR loop and it terminates when the implicit loop counter reaches its limit. This is but one example of how to write a loop in PL/SQL.
     
  3. tomloera

    tomloera Forum Expert

    Messages:
    167
    Likes Received:
    6
    Trophy Points:
    260
    Location:
    Monterrey Mexico
    David;

    Thanks a lot is good instruction.

    Regards
    Tom
     
  4. rajavu

    rajavu Forum Guru

    Messages:
    815
    Likes Received:
    52
    Trophy Points:
    610
    Location:
    @ Bangalore , India
    Btw, there are different kinds of Loops in Pl/SQL like basic Loop, For Loop, While Loop etc..

    Have a look at LOOPS