Syntax:
IF condition THEN
Statements;
[ELSIF condition
THEN
statements;]
[ELSE
statements;]
END
IF;
Note the indentation
above, failure to indent for readability will result in one full letter grade
deduction and your work being returned to you to fix.
Example:
ACCEPT p_empno PROMPT 'Enter
Employee #';
DECLARE
V_empno emp.empno%TYPE := &p_empno;
V_curr_sal emp.sal%TYPE;
V_avg_sal emp.sal%TYPE;
BEGIN
SELECT sal INTO v_curr_sal FROM emp
WHERE empno = v_empno;
SELECT AVG(sal) INTO
v_avg_sal FROM emp;
IF (v_curr_sal < 2000 OR
v_curr_sal < v_avg_sal) THEN
UPDATE emp SET sal = sal * 1.1
WHERE empno = v_empno;
ELSIF (v_curr_sal > 4500)
THEN
UPDATE emp
SET sal = sal * 1.2
WHERE empno = v_empno;
ELSE
UPDATE emp
SET sal = sal * 1.05
WHERE empno = v_empno;
END IF;
COMMIT WORK;
END;
/
Note the indentation
above, failure to indent for readability will result in one full letter grade
deduction and your work being returned to you to fix.
The following is identical to the above. You tell me which is
more readable!
ACCEPT p_empno PROMPT
'Enter Employee #';
DECLARE
V_empno
emp.empno%TYPE := &p_empno;
V_curr_sal emp.sal%TYPE;
V_avg_sal emp.sal%TYPE;
BEGIN
SELECT sal INTO
v_curr_sal FROM emp
WHERE empno = v_empno;
SELECT AVG(sal) INTO
v_avg_sal FROM emp;
IF (v_curr_sal < 2000 OR
v_curr_sal < v_avg_sal) THEN
UPDATE emp SET sal = sal *
1.1
WHERE empno = v_empno;
ELSIF (v_curr_sal > 4500) THEN
UPDATE emp SET sal = sal *
1.2
WHERE empno = v_empno;
ELSE
UPDATE emp SET sal = sal *
1.05
WHERE empno = v_empno;
END IF;
COMMIT WORK;
END;
/
-
A simple Loop
LOOP
Statement1;
Statement2;
EXIT [WHEN condition];
END
LOOP;
Continues to loop until the EXIT condition is met or executed.
Note the indentation
above, failure to indent for readability will result in one full letter grade
deduction and your work being returned to you to fix.
Example:
ACCEPT p_ord_id PROMPT
'Enter Order ID:';
DECLARE
V_ord_id item.ordid%TYPE := &p_ord_id;
V_line_item item.itemid%TYPE := 1;
BEGIN
LOOP
INSERT INTO item (ordid, itemid)
VALUES (v_ord_id, v_line_item);
V_line_item := v_line_item +1;
END LOOP;
END;
/
Note the indentation
above, failure to indent for readability will result in one full letter grade
deduction and your work being returned to you to fix.
Labeled
Loops:
<<outer_loop>>
LOOP
-- Some commands...
<<inner_loop>>
LOOP
-- Some more
commands...
<<inner_most_loop>>
LOOP
-- Some more
commands...
EXIT
outer_loop;
END LOOP
inner_most_loop;
-- Some more commands...
END LOOP
inner_loop;
END LOOP outer_loop;
Numeric For Loop:
Syntax:
FOR index IN [REVERSE] lower..upper
LOOP
Statement1;
Statement2;
END LOOP;
Example:
ACCEPT p_ord_id PROMPT
'Enter Order ID:';
DECLARE
V_ord_id item.ordid%TYPE := &p_ord_id;
BEGIN
FOR v_index IN 1..10 LOOP
INSERT INTO item (ordid, itemid)
VALUES (v_ord_id, v_index);
END LOOP;
END;
/
Note the indentation
above, failure to indent for readability will result in one full letter grade
deduction and your work being returned to you to fix.
While Loop:
Syntax:
WHILE condition LOOP
Statement1;
Statement2;
END LOOP;
Example:
ACCEPT p_ord_id PROMPT
'Enter Order ID:';
DECLARE
V_ord_id item.ordid%TYPE := &p_ord_id;
V_line_item item.itemid%TYPE := 1;
BEGIN
WHILE v_line_item <= 10 LOOP
INSERT INTO item (ordid, itemid)
VALUES (v_ord_id, v_line_item);
V_line_item := v_line_item +1;
END LOOP;
END;
/
Note the indentation
above, failure to indent for readability will result in one full letter grade
deduction and your work being returned to you to fix.
Goto Statement:
Syntax:
<<label_name>>
statement1;
statement2;
GOTO label_name;
-
Don?t
use GOTO unless absolutely necessary
-
Don?t
branch into an IF statement, LOOP, or sub-block
Example:
ACCPET p_ord_id PROMPT
'Enter Order ID:';
DECLARE
V_ord_id item.ordid%TYPE := &p_ord_id;
V_line_item item.itemid%TYPE := 1;
BEGIN
<<repeat>>
INSERT INTO item (ordid, itemid)
VALUES (v_ord_id, v_line_item);
V_line_item := v_line_item +1;
IF v_line_item <= 10 THEN
GOTO repeat;
END IF;
END;
/
Note the indentation
above, failure to indent for readability will result in one full letter grade
deduction and your work being returned to you to fix.
NO CONTINUE STATEMENT in Oracle through version 10g.
continue statement added to 11g.
while (condition == true)
{
if (this==that)
continue;
}
WHILE condition = true LOOP
IF this=that THEN
GOTO end_of_loop;
-- This helps to emulate a contiue
statement.
END IF;
<<end_of_loop>> -- This helps to emulate a contiue statement.
END LOOP;