• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Oracle: Control Structures
 

Oracle: Control Structures

on

  • 2,181 views

Oracle: Control Structures

Oracle: Control Structures

Statistics

Views

Total Views
2,181
Views on SlideShare
2,178
Embed Views
3

Actions

Likes
1
Downloads
0
Comments
0

1 Embed 3

http://www.slideshare.net 3

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Oracle: Control Structures Oracle: Control Structures Presentation Transcript

    • 1
      Using ORACLE®
      LOOPS and CONTROL structures
    • 2
      CONTROL STRUCTURES
      Control structures are those constructs that alter the flow of program execution.
    • 3
      LOOPS
      Control structures are those constructs that alter the flow of program execution.
      Loop is one such control structure. A loop iterates a given set of statements until a condition is satisfied.
      There are three types of looping constructs:
      Simple loop.
      While loop
      For loop.
      FOR counter IN
      Lwr_bnd..Upr_bnd
      LOOP
      Statement 1;
      ……
      ……
      END LOOP
      loop
      Statement 1;
      …….
      …….
      EXIT WHEN[condition]
      END LOOP
      WHILE( condition) LOOP
      Statement 1
      ……..
      …….
      END LOOP
    • 4
      SIMPLE LOOP
      A simple loop begins with a LOOP keyword, ends with a END LOOP keyword and in between are the statements to be performed. We must include an EXIT WHEN statement to set the exit condition or else it will form an infinite loop.
      EXAMPLE:
      DECLARE ename VARCHAR2(20); counter NUMBER :=40;
      BEGIN
      LOOP
      SELECT age INTO eage FROM InfoTable WHERE
      age = counter;
      DBMS_OUTPUT.PUT_LINE(‘Age is’ ||eage); SYNTAX
      counter += 5;
      EXIT WHEN counter >50;
      END LOOP
      END
      loop
      Statement 1;
      …….
      …….
      EXIT WHEN[condition]
      END LOOP
    • 5
      WHILE LOOP
      The WHILE loop is an entry control loop meaning that the condition is checked while entering the loop unlike the Simple loop where condition is checked while exit.
      EXAMPLE:
      DECLARE ename VARCHAR2(20); counter NUMBER :=40;
      BEGIN
      WHILE counter <55
      LOOP
      SELECT age INTO eage FROM InfoTable WHERE
      age = counter;
      DBMS_OUTPUT.PUT_LINE(‘Age is’ ||eage); SYNTAX
      counter += 5;
      END LOOP
      END
      WHILE( condition) LOOP
      Statement 1
      ……..
      …….
      END LOOP
    • 6
      LOOPS
      The FOR loop is an entry control loop with a LOWER_BOUND.. UPPER_BOUND specified in the condition in the same format. The counter variable will be implicitly declared.
      EXAMPLE:
      DECLARE
      ename VARCHAR2(20);
      eage NUMBER := 40;
      BEGIN
      FOR i IN 40..55
      LOOP
      select name INTO ename FROM Infotable WHERE age = eage; SYNTAX
      eage := eage+5;
      DBMS_OUTPUT.PUT_LINE('Name is'||ename);
      END LOOP;
      END
      FOR counter IN
      Lwr_bnd..Upr_bnd
      LOOP
      Statement 1;
      ……
      ……
      END LOOP
    • 7
      NESTED LOOPS
      We can nest one loop inside another by giving labels to the loops.
      SYNTAX
      <<outer_loop>>label for outer loop
      LOOP
      <<inner_loop>> label for inner loop
      LOOP Statement 1;
      …..
      EXIT WHEN [cond]
      END LOOP inner _loop;
      Statement 1;
      ……
      ……
      EXIT WHEN [cond]
      END LOOP outer_loop;
      <<outer_loop>>
      FOR counter IN
      Lwr_bnd..Upr_bnd
      LOOP
      <<inner_loop>
      LOOP
      Statement 1;
      …..
      EXIT WHEN [cond]
      END LOOP Inner _loop;
      Statement 1;
      ……
      ……
      END LOOP Outer_loop;
    • 8
      SELECTIVE STATEMENTS
      Selective statements are the constructs that perform the action based on the condition that is satisfied.There are four selective constructs:
      IF
      IF…..ELSE
      IF……ELSIF……ELSE
      CASE
      IF (condition..)
      THEN
      Statement1;
      Statement2;
      ELSIF
      Statement1;
      Statement2;
      ELSE
      Statement1;
      Statement2;
      END IF;
      IF (condition..)
      THEN
      Statement1;
      Statement2;
      ……
      END IF;
      IF (condition..)
      THEN
      Statement1;
      Statement2;
      ELSE
      Statement1;
      Statement2;
      END IF;
      CASE selector
      WHEN expression 1 THEN result 1;
      WHEN expression 2 THEN result 2;
      …….
      ELSE [result n]
      END
    • 9
      IF
      The IF statement has a condition ,which when satisfied the statements in its body are executed else the are not executed.
      EXAMPLE:
      VARIABLE ename VARCHAR2(20);
      BEGIN
      ename :=&ename;
      IF ename=‘Bill‘ THEN
      DBMS_OUTPUT.PUT_LINE('Hi BILL!Welcome');
      END IF;
      END
      The above code outputs “Hi BILL!Welcome” if the user enters the name as Bill else no output is shown.
      IF (condition..)
      THEN
      Statement1;
      Statement2;
      ……
      END IF;
    • 10
      IF….ELSE
      The IF statement provides only the action to be taken if condition is satisfied .
      But the IF…ELSE construct provides both the actions to be taken when the condition is satisfied given in the IF block and the action when it is not which is given in the ELSE block.
      EXAMPLE:
      VARIABLE ename VARCHAR2(20);
      BEGIN
      ename :=&ename;
      IF ename='bill‘ THEN
      DBMS_OUTPUT.PUT_LINE('Hi BILL!Welcome');
      ELSE
      DBMS_OUTPUT.PUT_LINE(‘ Sorry!Access only to bill ');
      END IF;
      END
      Here the output is 'Hi BILL! Welcome’ when user enters Bill and displays the message “Sorry! Access only to bill “ if the user enters any other name.
      IF (condition..)
      THEN
      Statement1;
      Statement2;
      ELSE
      Statement1;
      Statement2;
      END IF;
    • 11
      IF…ELSIF…ELSE
      The IF statement provides only the action to be taken if condition is satisfied .
      But the IF…ELSEIF…ELSE construct provides both the actions to be taken when one the condition ,another action if another condition is satisfies and finally the action is both conditions fail.
      EXAMPLE:
      VARIABLE ename VARCHAR2(20);
      BEGIN
      ename :=&ename;
      IF ename=‘Bill‘ THEN
      DBMS_OUTPUT.PUT_LINE('Hi BILL!Welcome');
      ELSIF ename= ‘Steve’ THEN
      DBMS_OUTPUT.PUT_LINE(‘Hi Steve! Come on in');
      ELSE
      DBMS_OUTPUT.PUT_LINE(‘ Sorry!Access only to bill or steve ');
      END IF;
      END
      Here the output is 'Hi BILL! Welcome’ when user enters Bill, output is 'Hi Steve! Come on in’ when user enters Steve and displays the message "Sorry! Access only to bill “ if the user enters any other name.
      IF (condition..)
      THEN
      Statement1;
      Statement2;
      ELSIF
      Statement1;
      Statement2;
      ELSE
      Statement1;
      Statement2;
      END IF;
    • 12
      CASE
      The CASE construct is a special form of the ELSIF construct which matches a selector values with list of expresions and if either is matched the executes those statements.
      EXAMPLE:
      VARIABLE ename VARCHAR2(20);
      BEGIN
      ename :=&ename;
      CASE ename
      WHEN ‘Bill’ THEN DBMS_OUTPUT.PUT_LINE(‘Hi Bill! Come on in');
      WHEN ‘Steve’ THEN DBMS_OUTPUT.PUT_LINE(‘Hi Steve! Come on in');
      WHEN ‘Larry’ THEN DBMS_OUTPUT.PUT_LINE(‘Hi Larry! Come on in’);
      ELSE DBMS_OUTPUT.PUT_LINE(‘ Sorry!Access only to bill or steve or larry ');
      END IF;
      END
      Here the output is 'Hi BILL! Come on in’ when user enters Bill, output is 'Hi Steve! Come on in’ when user enters Steve and output is 'Hi Larry! Come on in’ when user enters Larry and displays the message "Sorry! Access only to bill “ if the user enters any other name.
      CASE selector
      WHEN expression 1 THEN result 1;
      WHEN expression 2 THEN result 2;
      …….
      ELSE [result n]
      END
    • THANK YOU
      13
      THANK YOU FOR VIEWING THIS PRESENTATION
      FOR MORE PRESENTATIONS AND VIDEOS ON ORACLE AND DATAMINING ,
      please visit:
      www.dataminingtools.net