Friday, 4 April 2014

Loop and If-Else



There are basically two types of loop:
1)      For Loop
2)      While Loop

For Loop:-
                For loop is for iteration purpose with some certain condition.
                Syntax:-
FOR incremnet IN val1..val2
  LOOP
          statements;
END LOOP;
Note: - Here val1 - Start integer value and val2 - End integer value.

Example:-
FOR i IN (SELECT emp_id
                  FROM   emp
                  WHERE  emp_name = '%kumar%')
  LOOP
   Insert into emp_temp values(i.emp_id, emp_name, emp_sal);
  END LOOP;

While Loop:-
            A while Loop is used when a set of statements is getting executed with some certain condition. The iteration continues until the condition becomes false.
Syntax:-
While <condition>
    LOOP statements;
END LOOP;

Example:-
                    i integer :=1;
                    while i < 10
                     LOOP
                                         Insert into emp values(i, emp_name, emp_sal);
 
                    END LOOP;

No comments:

Post a Comment