While loop and number counter : While Loop « PL SQL « Oracle PL / SQL






While loop and number counter

    

SQL> -- create demo table
SQL> create table emp(
  2    ID                 VARCHAR2(4 BYTE)         NOT NULL,
  3    fname         VARCHAR2(10 BYTE),
  4    lname          VARCHAR2(10 BYTE),
  5    Start_Date         DATE,
  6    End_Date           DATE,
  7    Salary             Number(8,2),
  8    City               VARCHAR2(10 BYTE),
  9    Description        VARCHAR2(15 BYTE)
 10  )
 11  /

Table created.

SQL>
SQL>
SQL>
SQL> -- prepare data
SQL> insert into emp(ID,  fname, lname, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2               values ('01','Jason',    'Martin',  to_date('19960725','YYYYMMDD'), to_date('20060725','YYYYMMDD'), 1234.56, 'Toronto',  'Programmer')
  3  /

1 row created.

SQL> insert into emp(ID,  fname, lname, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2                values('02','Alison',   'Mathews', to_date('19760321','YYYYMMDD'), to_date('19860221','YYYYMMDD'), 6661.78, 'Vancouver','Tester')
  3  /

1 row created.

SQL>
SQL>
SQL> declare
  2    s Number(8,2);
  3    totalValue Number(8,2);
  4    begin
  5         WHILE totalValue < 100 LOOP
  6              Select salary into s from emp;
  7              totalValue := totalValue + s;
  8         END LOOP;
  9
 10         dbms_output.put_line(totalValue);
 11  end;
 12  /

PL/SQL procedure successfully completed.

SQL>
SQL> drop table emp;

Table dropped.

SQL>

   
    
    
    
  








Related examples in the same category

1.Put two statement in while loop
2.Example of a WHILE loop that never executes
3.Corrected WHILE loop that executes
4.While Loop with condition
5.Change while loop counter
6.While loop with complex conditions
7.Insert value in while loop
8.Do calculation with while loop counter
9.Call EXIT to jump out of a while loop
10.Use EXIT WHEN to exit a while loop
11.WHILE..LOOP, Cursor Loop
12.WHILE Loop: This condition will evaluate to NULL, since v_Counter is initialized to NULL by default
13.WHILE Loop: Test the loop counter before each loop iteration to insure that it is still less than 50
14.WHILE Loop
15.This script demonstrates variable visibility
16.The WHILE loop uses a loop index value as its gate on entry criterion: