Oracle: SQL*PLUS - Simple Loops

Syntax | Example

 

Syntax

LOOP

Statements;

EXIT [condition] // To avoid infinite execution

END LOOP;

 

Example # 1

The following block inserts 10 (ten) rows into emp table using Simple Loop

declare

      no number(10):=1;

begin

loop
        insert into emp(empno)
        Values (no);
        no:=no+1;

exit when no>10;
end loop;

end;
/

 

Example # 2

 - Note carefully the use of 'rollback'

 - Loop executes 4 times but rowcount value remains the same . i.e. 14

declare


    no number(10):=0;
    id number(10);
    name varchar2(100):=NULL;

begin


    select sal,deptno,ename into no, id, name
    from emp
    where empno=7844;

loop

    dbms_output.put_line(no||id||name);
    delete from emp
    where empno=7844;
    rollback;
    update emp
    set sal=sal+2;

    dbms_output.put_line(' Executing row count statement');

    dbms_output.put_line(SQL%rowcount);

    rollback;
    no:=no*2;

    dbms_output.put_line(' Here no = '||no);

exit when no>20000;


end loop;

end;
/

ORACLE - PL/SQL

Database - SQL* PLUS - Primay key (1 column)
Database - SQL* PLUS - Crete TABLE
Database - SQL* PLUS - For Loops
Database - SQL* PLUS - Primary key (two-column)
Oracle PL/SQL - Procedures
Database - SQL* PLUS - Simple Loop
Database - SQL* PLUS - While Loops

 

 

 

 

 

 

 

 

 

Custom Search
 

Home      Disclaimer      Advertise      Contact      Privacy Policy     

Copyright © 2004-10 Paked.com. All rights reserved.

Note: Site best viewed at 1024 x 768 or higher screen resolution