Ex - No: 6 Date:16.02.2019 Write A PL/SQL Block To Satisfy Some Conditions by Accepting Input From The User
Ex - No: 6 Date:16.02.2019 Write A PL/SQL Block To Satisfy Some Conditions by Accepting Input From The User
no: 6
Date:16.02.2019
Write a PL/SQL block to satisfy some conditions by accepting input from the
user
Aim:
To implement a PL/SQL block to satisfy some conditions by accepting input from the user.
Concepts Involved:
PL/SQL Placeholders:
Placeholders are temporary storage area. Placeholders can be any of Variables, Constants and
Records. Oracle defines placeholders to store data temporarily, which are used to manipulate data
during the execution of a PL SQL block.
Depending on the kind of data you want to store, you can define placeholders with a name and
a datatype. Few of the datatypes used to define placeholders are as given below.
Number (n,m) , Char (n) , Varchar2 (n) , Date , Long , Long raw, Raw, Blob, Clob, Nclob, Bfile
PL/SQL Variables:
These are placeholders that store the values that can change through the PL/SQL Block.
General Syntax to declare a variable is
variable_name datatype [NOT NULL := value ];
variable_name is the name of the variable.
datatype is a valid PL/SQL datatype.
NOT NULL is an optional specification on the variable.
value or DEFAULT valueis also an optional specification, where you
can initialize a variable.
Each variable declaration is a separate statement and must be terminated by a
semicolon.
For example, if you want to store the current salary of an employee, you can use a variable.
DECLARE
salary number (6);
Example: The below program will get the salary of an employee with id '1116' and display it on the
screen.
DECLARE
var_salary number(6);
var_emp_id number(6) = 1116;
BEGIN
SELECT salary
INTO var_salary
FROM employee
WHERE emp_id = var_emp_id;
dbms_output.put_line(var_salary);
dbms_output.put_line('The employee '
|| var_emp_id || ' has salary ' || var_salary);
END;
/
NOTE: The backward slash '/' in the above program indicates to execute the above PL/SQL Block.
4)
IF condition1 THEN
ELSE
IF condition2 THEN
statement1;
END IF;
ELSIF condition3 THEN
statement2;
END IF;
Iterative Statements in PL/SQL:
Iterative control Statements are used when we want to repeat the execution of one or more
statements for specified number of times.
There are three types of loops in PL/SQL.
1. Simple Loop
2. While Loop
3. For Loop
1. Simple Loop
A Simple Loop is used when a set of statements is to be executed at least once before the loop
terminates. An EXIT condition must be specified in the loop, otherwise the loop will get into an infinite
number of iterations. When the EXIT condition is satisfied the process exits from the loop.
General Syntax to write a Simple Loop is
:
LOOP
statements;
EXIT;
{or EXIT WHEN condition;}
END LOOP;
2. While Loop
A WHILE LOOP is used when a set of statements has to be executed as long as a condition is
true. The condition is evaluated at the beginning of each iteration. The iteration continues until the
condition becomes false.
The General Syntax to write a WHILE LOOP is:
WHILE <condition>
LOOP statements;
END LOOP;
3. FOR Loop
A FOR LOOP is used to execute a set of statements for a predetermined number of times.
Iteration occurs between the start and end integer values given. The counter is always incremented by
1. The loop exits when the counter reaches the value of the end integer.
The General Syntax to write a FOR LOOP is:
FOR counter IN val1..val2
LOOP statements;
END LOOP;
val1 - Start integer value.
val2 - End integer value.
Important steps to follow when executing a while loop:
1) The counter variable is implicitly declared in the declaration section, so it's not necessary to
declare it explicitly.
2) The counter variable is incremented by 1 and does not need to be incremented explicitly.
3) EXIT WHEN statement and EXIT statements can be used in FOR loops but it's not done
oftenly.
begin
n:=&n;
for i in 1..n
loop
fac:=fac*i;
end loop;
dbms_output.put_line('factorial='||fac);
end;
OUTPUT:
2. Write a PL/SQL block which will accept marks of three subjects, namely maths, physics and
chemistry. Calculate the average marks and display the total grade as per the following
conditions:
IF average marks<40 then Grade C
IF average marks between 40 and 70 then Grade B
ELSE Grade A
PROGRAM:
set serveroutput on;
declare
maths number(10);
physics number(10);
chemistry number(10);
total number(10);
avgs number(10);
begin
maths:=&maths;
physics:=&physics;
chemistry:=&chemistry;
total:=(maths+physics+chemistry);
avgs:=(total/3);
if avgs<40 then
dbms_output.put_line('Grade C');
dbms_output.put_line('GRADE B');
else
dbms_output.put_line('GRADE A');
end if;
dbms_output.put_line('AVERAGE IS '||avgs);
end;
OUTPUT:
RESULT:
In this a PL/SQL block to satisfy some conditions by accepting input from the user is
implemented and the output was verified successfully