NiC IT Academy

PLSQL Introduction – Day – 04

Published On: 9 September 2024

Last Updated: 16 September 2024

No Responses

1) Conditional statement in PLSQL:
===============================

— IF Statement
— Case Statement

IF Statement:
=============

IF condition then
statement;
end IF;

—————————————————————-

IF condition1 THEN
[statements to execute when condition1 is TRUE…]

ELSE
[statements to execute when condition1 is FALSE]

END IF;

———————————————————————

IF condition1 THEN
[statements to execute when condition1 is TRUE…]

ELSIF condition2 THEN
[statements to execute when condition2 is TRUE…]

ELSE
[statements to execute when both condition1 and condition2 are FALSE]

END IF;

—————————————————-
Example 1:

declare
a boolean:=False;
begin
if a then
dbms_output.put_line(‘The condition is true’);
else
dbms_output.put_line(‘The condition is false’);
end if;
end;

declare
a number:=10;
b number:=5;
begin
if a>b then
dbms_output.put_line(a ||’ is greater than ‘||b);
else
dbms_output.put_line(b ||’ is greater than ‘||a);
end if;
end;

————————————————————————-

Example 2:

DECLARE
a boolean := true;
b boolean := false;
BEGIN
IF (a AND b) THEN
dbms_output.put_line(‘Line 1 – Condition is true’);
else
dbms_output.put_line(‘Line 2 – Condition is true’);
END IF;

END;
————————————————————————-
Example 3:

DECLARE
a boolean := true;
b boolean := false;
BEGIN
IF (a AND b) THEN
dbms_output.put_line(‘Line 1’);
END IF;
IF (a OR b) THEN
dbms_output.put_line(‘Line 2’);
END IF;
IF (NOT a) THEN
dbms_output.put_line(‘Line 3’);
ELSE
dbms_output.put_line(‘Line 4’);
END IF;
IF (NOT b) THEN
dbms_output.put_line(‘Line 5’);
ELSE
dbms_output.put_line(‘Line 6’);
END IF;
END;

Example 4:

declare
v_max_salary number(10);
begin
select max(salary) into v_max_salary
from employees;
dbms_output.put_line(‘The maximum salary’||v_max_salary);
IF v_max_salary>100000 then
delete from employees where salary=v_max_salary;
end if;
end;

===============================================================================
————————————————————-
CASE Statement

CASE selector
WHEN selector_value_1 THEN
statements_1
WHEN selector_value_1 THEN
statement_2

ELSE
else_statements
END CASE;


declare
vsalary number(10);
vlocal number(10):=&vemp_id;
begin
select salary into vsalary from employees where employee_id=vlocal;
case vsalary
when vsalary > 15000 then
dbms_output.put_line(‘Fair salary’);
when vsalary > 10000 and vsalary < 15000 then
dbms_output.put_line(‘Avg salary’);
else
dbms_output.put_line(vsalary||’ Low salary’);
end case;

end;

 

————————————————————-

–searched case:

The searched CASE statement evaluates multiple Boolean expressions and chooses the first one whose value is TRUE . Expression whose value is evaluated once and used to select one of several alternatives. selector can have any PL/SQL data type except BLOB , BFILE , or a user-defined type.

CASE
WHEN condition_1 THEN statements_1
WHEN condition_2 THEN statements_2

WHEN condition_n THEN statements_n
[ ELSE
else_statements ]
END CASE;]

Example:

declare
vsalary number(10);
begin
select salary into vsalary from employees where employee_id=101;
case
when vsalary > 15000 and vsalary < 20000 then
dbms_output.put_line(‘Fair salary’);
when vsalary > 5000 and vsalary < 10000 then
dbms_output.put_line(‘low salary’);
else
dbms_output.put_line(vsalary ||’high salary’);
end case;
end;

===================================================================

— case statement in SQL

select cust_id,cust_name,mobile_no,dob,city,country,
case
when initcap(country)=’India’ then ‘+91-‘||mobile_no
when country=’USA’ then ‘+1-‘||mobile_no
when country=’Singapore’ then ‘+65-‘||mobile_no
else mobile_no
end new_phone_no from customer;

====================================================================

Simple CASE or searched CASE statement:

As a rule of thumb, use a searched CASE statement when you want to execute a sequence of statements based on the results of multiple Boolean expressions and use a simple CASE statement when you want to execute a sequence of statements based on the result of a single expression.

2) LOOPING Statements in PLSQL

–1. Simple Loop: 
Loop … end loop
syntax:
    Loop
    increment
    exit when condition
end loop
–2. While Loop 
while (condition) loop …… end loop;
–3. For Loop 
For …..Loop… end loop;
——————————————————————
declare
c number:=1;
begin
loop
dbms_output.put_line(‘Welcome to PLSQL ‘||c);
c:=c+1;
exit when c>5;
end loop;
end;
—  to print even numbers 0 to 10
declare
c number:=0;
begin
loop
c:=c+2;
dbms_output.put_line(c);
exit when c>=10;
end loop;
end;
declare
c number:=1;
begin
loop
dbms_output.put_line(‘Page ‘||c||’ of 50′);
c:=c+1;
exit when c>50;
end loop;
end;
—————————————————————-
WHILE Loop
While (condition)
loop
executable statements;
increment
end loop;
declare
c number:=0;
begin
while(c<=5)
loop
dbms_output.put_line(‘Hello’);
c:=c+1;
end loop;
end;
—————————————————————————————-
 FOR LOOP
FOR variable in [reverse] low value .. high value
loop
executable statements;
end loop;
declare
c1 number;
begin
for c1 in 1..5
loop
dbms_output.put_line(‘Hello’);
end loop;
end;
————————————————————————————
Example 1: 
In this example, we are going to print number starting from 1 using Basic loop statement. Each number will be printed as many times as its value. The upper limit of the series is fixed at the program declaration part. Let us learn how we can use the label concept to achieve this. For that, we will execute the following code
DECLARE
a NUMBER:=0;
b NUMBER:=0;
upper_limit NUMBER :=4;
BEGIN
dbms_output.put_line(‘Program started.’ );
–outerloop
LOOP
a:=a+1;
b:=1;
–inner loop
LOOP
EXIT  WHEN a > upper_limit;
dbms_output.put_line(a);
b:=b+1;
EXIT  WHEN b>a;
END LOOP;
END LOOP;
dbms_output.put_line(‘Program completed.’);
END;
/

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *

Login with your email & password

Sign up with your email & password

Signup/Registration Form

Registred Email:

- Not Updated -

Set/Update Password