Tuesday, October 21, 2014

IF CONDITION IN THE PL/SQL BLOCK



Selection: means the execution of statements depending upon a condition test. If condition evaluates to true, set of statements is followed otherwise different set of statements is followed. This is also known as decision construct.
If..end if: this is the simplest form of the IF statement. If the condition between IF and THEN us true then the set of statements between the THEN and END IF gets executed otherwise not executed.

If   then


.


.


end if;


Write a program to input the salary and working exp of emp and calculate the bonus as 10% of salary. Give 500 RS. Extra bonus to those whose working exp. More than 10 years.
DECLARE
SAL NUMBER;
BONUS NUMBER;
YEARS NUMBER;
BEGIN
SAL := &SALARY;
YEARS := &WORKING_YEARS;
BONUS := SAL * .1;
IF YEARS > 10 THEN
BONUS := BONUS + 500;
END IF;
DBMS_OUTPUT.PUT_LINE(‘BONUS: ‘ || BONUS);
END;

If..else..end if: 

in this construct If the condition between IF and THEN us true then the set of statements between the THEN and ELSE gets executed otherwise the set of statements between ELSE and END IF gets executed.

if then
.
else
.
end if;

Write a PL/SQL prog to input two no’s and print the maximum no between them.
DECLARE
N1 NUMBER;
N2 NUMBER;
BEGIN
N1 := &NO1;
N2 := &NO2;
IF N1 > N2 THEN
DBMS_OUTPUT.PUT_LINE(‘NO1 IS GREATER’);
ELSE
DBMS_OUTPUT.PUT_LINE(‘NO2 IS GREATER’);
END IF;
END;

Multiple IF: 
this is last and most complex form of IF statement. It executes the code following a that evaluates to TRUE. If no condition evaluates to true the code following ELSE gets executed.
if then
.
elsIf   then
.
else
.
end if;

Write a PL/SQL program to input the Basic Salary and calculate the HRA, DA and Net Salary as per:
BASIC            HRA               DA
>15000            12%                8%
>12000            10%                6%
>9000              7%                  4%
OTHERS       5%                  200/-
DECLARE
BASIC NUMER
HRA NUMBER;
DA NUMBER;
NET NUMBER;
BEGIN
BASIC := &BASIC_SALARY;
IF BASIC > 15000 THEN
HRA := BASIC * .12;
DA := BASIC * .08;
ELSIF BASIC > 12000 THEN
HRA := BASIC * .1;
DA := BASIC * .06;
ELSIF BASIC > 9000 THEN
HRA := BASIC * ..07;
DA := BASIC * .04;
ELSE
HRA := BASIC * .05;
DA := BASIC * 200;
END IF;
NET := BASIC + HRA + DA;
DBMS_OUTPUT.PUT_LINE(‘BASIC: ‘ || BASIC);
DBMS_OUTPUT.PUT_LINE(‘HRA: ‘ || HRA);
DBMS_OUTPUT.PUT_LINE(‘DA: ‘ || DA);
DBMS_OUTPUT.PUT_LINE(‘NET: ‘ || NET);
END;
Nested Ifs: when IF statement can contain another IF inside it is called NESTED Ifs.

if then
.
IF CONDITION THEN
.
ELSE
.
IF CONDITION THEN
.
END IF;
.
END IF;
END IF;

Points to remember for using IF

Ø  Always match up an IF with an END IF.
Ø  Must put a space between the keywords END and IF.
Ø  The ELSIF keyword does not have an embedded “E”.
Ø  Place a semicolon (;) only after the END IF keyword.
 

No comments: