In PL/SQL, the conditional controls are
1) IF
2) CASE
3) GOTO AND LABEL
1) IF CONDITIONAL CONTROL
The IF syntax is as follows
i) Simple IF
IF <CONDITION>
THEN
-- If Condition True Then This statements will be executed
END IF;
ii) IF - ELSE
IF <CONDITION>
THEN
-- If Condition True Then This statement will be executed
ELSE
-- If the above Condition false Then This statement will be executed
END IF;
iii) IF - ELSIF
IF <CONDITION1>
THEN
-- If Condition1 True Then This statement will be executed
ELSIF <CONDITION2>
-- If Condition2 True Then This statement will be executed
END IF;
iv) IF - ELSIF - ELSE
IF <CONDITION1>
THEN
-- If Condition1 True Then This statement will be executed
ELSIF <CONDITION2>
-- If Condition2 True Then This statement will be executed
ELSE
-- If the above 2 Conditions false Then This statement will be executed
END IF;
v) NESTED IF
IF <CONDITION1>
THEN
-- If Condition1 True Then This statements will be executed
IF <CONDITION2>
THEN
-- If Condition2 True Then This statements will be executed
END IF;
END IF;
Example:
DECLARE
V_NUM1 NUMBER:= 100;
V_NUM2 NUMBER:= 200;
BEGIN
IF V_NUM1 > V_NUM2 THEN
DBMS_OUTPUT.PUT_LINE('NUM1 IS BIGGER THAN NUM2');
ELSE
DBMS_OUTPUT.PUT_LINE('NUM2 IS BIGGER THAN NUM1');
END IF;
END;
2) CASE
The Case syntax is as follows
i)
CASE V_VALUE
WHEN VAL1 THEN
-- IF V_VALUE = VAL1 THEN THIS STATEMENT WILL BE EXECUTED
WHEN VAL2
-- IF V_VALUE = VAL2 THEN THIS STATEMENT WILL BE EXECUTED
ELSE
-- OTHERWISE THIS STATEMENT WILL BE EXECUTED
END CASE;
ii)
CASE
WHEN VAL1= V_VALUE THEN
-- If VAL1 = V_VALUE is true then this statement will be executed
WHEN VAL2= V_VALUE THEN
-- If VAL1 = V_VALUE is true then this statement will be executed
ELSE
-- OTHERWISE THIS STATEMENT WILL BE EXECUTED
END CASE;
Example:
DECLARE
V_VALUE NUMBER := 10;
V_VAL1 NUMBER := 20;
V_VAL2 NUMBER := 10;
BEGIN
CASE
WHEN V_VALUE=V_VAL1 THEN
DBMS_OUTPUT.PUT_LINE('V_VALUE = V_VAL1 IS TRUE');
WHEN V_VALUE=V_VAL2 THEN
DBMS_OUTPUT.PUT_LINE('V_VALUE = V_VAL2 IS TRUE');
ELSE
DBMS_OUTPUT.PUT_LINE('V_VALUE EQUAL VALUE VARIABLE NOT FOUND');
END CASE;
3) GOTO AND LABEL
To declare the label in the PL/SQL block is as follows
<<Label_Name>>
That's it.
If we want to go to the lable location just need to use the following statement
GOTO Lable_name;
1) IF
2) CASE
3) GOTO AND LABEL
1) IF CONDITIONAL CONTROL
The IF syntax is as follows
i) Simple IF
IF <CONDITION>
THEN
-- If Condition True Then This statements will be executed
END IF;
ii) IF - ELSE
IF <CONDITION>
THEN
-- If Condition True Then This statement will be executed
ELSE
-- If the above Condition false Then This statement will be executed
END IF;
iii) IF - ELSIF
IF <CONDITION1>
THEN
-- If Condition1 True Then This statement will be executed
ELSIF <CONDITION2>
-- If Condition2 True Then This statement will be executed
END IF;
iv) IF - ELSIF - ELSE
IF <CONDITION1>
THEN
-- If Condition1 True Then This statement will be executed
ELSIF <CONDITION2>
-- If Condition2 True Then This statement will be executed
ELSE
-- If the above 2 Conditions false Then This statement will be executed
END IF;
v) NESTED IF
IF <CONDITION1>
THEN
-- If Condition1 True Then This statements will be executed
IF <CONDITION2>
THEN
-- If Condition2 True Then This statements will be executed
END IF;
END IF;
Example:
DECLARE
V_NUM1 NUMBER:= 100;
V_NUM2 NUMBER:= 200;
BEGIN
IF V_NUM1 > V_NUM2 THEN
DBMS_OUTPUT.PUT_LINE('NUM1 IS BIGGER THAN NUM2');
ELSE
DBMS_OUTPUT.PUT_LINE('NUM2 IS BIGGER THAN NUM1');
END IF;
END;
2) CASE
The Case syntax is as follows
i)
CASE V_VALUE
WHEN VAL1 THEN
-- IF V_VALUE = VAL1 THEN THIS STATEMENT WILL BE EXECUTED
WHEN VAL2
-- IF V_VALUE = VAL2 THEN THIS STATEMENT WILL BE EXECUTED
ELSE
-- OTHERWISE THIS STATEMENT WILL BE EXECUTED
END CASE;
ii)
CASE
WHEN VAL1= V_VALUE THEN
-- If VAL1 = V_VALUE is true then this statement will be executed
WHEN VAL2= V_VALUE THEN
-- If VAL1 = V_VALUE is true then this statement will be executed
ELSE
-- OTHERWISE THIS STATEMENT WILL BE EXECUTED
END CASE;
Example:
DECLARE
V_VALUE NUMBER := 10;
V_VAL1 NUMBER := 20;
V_VAL2 NUMBER := 10;
BEGIN
CASE
WHEN V_VALUE=V_VAL1 THEN
DBMS_OUTPUT.PUT_LINE('V_VALUE = V_VAL1 IS TRUE');
WHEN V_VALUE=V_VAL2 THEN
DBMS_OUTPUT.PUT_LINE('V_VALUE = V_VAL2 IS TRUE');
ELSE
DBMS_OUTPUT.PUT_LINE('V_VALUE EQUAL VALUE VARIABLE NOT FOUND');
END CASE;
3) GOTO AND LABEL
To declare the label in the PL/SQL block is as follows
<<Label_Name>>
That's it.
If we want to go to the lable location just need to use the following statement
GOTO Lable_name;
No comments:
Post a Comment