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!

plsql loops

Discussion in 'SQL PL/SQL' started by nallupradeepreddy, Jul 22, 2012.

  1. nallupradeepreddy

    nallupradeepreddy Active Member

    Messages:
    5
    Likes Received:
    1
    Trophy Points:
    65
    i want to loop the ename from emp table concatinated with 10 .i am unable to do it pls help me out......

    declare
    x constant number:=10;
    me emp.ename%TYPE;
    begin
    for me in (select emp.ename from emp)
    loop
    dbms_output.put_line(x||me);
    end loop;
    end;
     

    Attached Files:

    sambuduk likes this.
  2. zargon

    zargon Community Moderator Forum Guru

    Messages:
    2,347
    Likes Received:
    348
    Trophy Points:
    1,430
    Location:
    Aurora, CO
    You're creating a record variable, not a scalar variable, in your loop so you need to treat it as such. Please see the example posted below on how to reference such constructs:

    Code (SQL):
    SQL> DECLARE
      2  x constant NUMBER:=10;
      3  me emp.ename%TYPE;
      4  BEGIN
      5          FOR me IN (SELECT emp.ename FROM emp)
      6          loop
      7                  dbms_output.put_line(x||me.ename);
      8          END loop;
      9  END;
     10  /
    10SMITH
    10ALLEN
    10WARD
    10JONES
    10MARTIN
    10BLAKE
    10CLARK
    10SCOTT
    10KING
    10TURNER
    10ADAMS
    10JAMES
    10FORD
    10MILLER
     
    PL/SQL PROCEDURE successfully completed.
     
    SQL>
    SQL>
     
  3. nallupradeepreddy

    nallupradeepreddy Active Member

    Messages:
    5
    Likes Received:
    1
    Trophy Points:
    65
    thank you for u r reply....
     
  4. kiran.marla

    kiran.marla Forum Genius

    Messages:
    403
    Likes Received:
    52
    Trophy Points:
    505
    Location:
    Khammam
    There is no need of creating ME variable here.
    Code (SQL):
    SQL> ed
    Wrote file afiedt.buf

      1  DECLARE
      2      x constant NUMBER:=10;
      3      BEGIN
      4     FOR me IN (SELECT scott.emp.ename FROM scott.emp)
      5              loop
      6                      dbms_output.put_line(x||me.ename);
      7     END loop;
      8*    END;
    SQL> /
    10SMITH
    10ALLEN
    10WARD
    10JONES
    10MARTIN
    10BLAKE
    10CLARK
    10SCOTT
    10KING
    10TURNER
    10ADAMS
    10JAMES
    10FORD
    10MILLER

    PL/SQL PROCEDURE successfully completed.

    SQL>
     
  5. nallupradeepreddy

    nallupradeepreddy Active Member

    Messages:
    5
    Likes Received:
    1
    Trophy Points:
    65
    thank q ......
    kiran marla and zargon..