0% found this document useful (0 votes)
53 views

Ex - No: 6 Date:16.02.2019 Write A PL/SQL Block To Satisfy Some Conditions by Accepting Input From The User

The document describes how to create a PL/SQL block that accepts input from the user and satisfies conditions. It discusses PL/SQL placeholders like variables and constants, and conditional and iterative statements. It provides two examples - a block to calculate a factorial and a block to accept subject marks, calculate average, and return a grade. The aim is met by implementing a block that accepts user input and displays output based on conditional logic.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Ex - No: 6 Date:16.02.2019 Write A PL/SQL Block To Satisfy Some Conditions by Accepting Input From The User

The document describes how to create a PL/SQL block that accepts input from the user and satisfies conditions. It discusses PL/SQL placeholders like variables and constants, and conditional and iterative statements. It provides two examples - a block to calculate a factorial and a block to accept subject marks, calculate average, and return a grade. The aim is met by implementing a block that accepts user input and displays output based on conditional logic.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Ex.

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);

* “salary” is a variable of datatype number and of length 6.


When a variable is specified as NOT NULL, you must initialize the variable when it is declared.
For example: The below example declares two variables, one of which is a not null.
DECLARE
salary number(4);
dept varchar2(10) NOT NULL := “HR Dept”;
The value of a variable can change in the execution or exception section of the PL/SQL Block.
We can assign values to variables in the two ways given below.

1) We can directly assign values to variables.


The General Syntax is:
variable_name:= value;
2) We can assign values to variables directly from the database columns by using a SELECT.. INTO
statement.
The General Syntax is:
SELECT column_name
INTO variable_name
FROM table_name
[WHERE condition];

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.

Scope of PS/SQL Variables


PL/SQL allows the nesting of Blocks within Blocks i.e, the Execution section of an outer block
can contain inner blocks. Therefore, a variable which is accessible to an outer Block is also accessible
to all nested inner Blocks. The variables declared in the inner blocks are not accessible to outer blocks.
Based on their declaration we can classify variables into two types.
Local variables - These are declared in a inner block and cannot be referenced by outside
Blocks.
Global variables - These are declared in a outer block and can be referenced by its itself
and by its inner blocks.
so cannot be accessed in the outer block i.e. it cannot be accessed after line 11. The variables
'var_num1' and 'var_num2' can be accessed anywhere in the block.
1> DECLARE
2> var_num1 number;
3> var_num2 number;
4> BEGIN
5> var_num1 := 100;
6> var_num2 := 200;
7> DECLARE
8> var_mult number;
9> BEGIN
10> var_mult := var_num1 * var_num2;
11> END;
12> END;
13> /
PL/SQL Constants:
As the name implies a constant is a value used in a PL/SQL Block that remains unchanged
throughout the program. A constant is a user-defined literal value. You can declare a constant and use
it instead of actual value.
For example: If you want to write a program which will increase the salary of the employees
by 25%, you can declare a constant and use it throughout the program. Next time when you want to
increase the salary again you can change the value of the constant which will be easier than changing
the actual value throughout the program.
General Syntax to declare a constant is:
constant_name CONSTANT datatype := VALUE;
For example, to declare salary_increase, you can write code as follows:
DECLARE
salary_increase CONSTANT number (3) := 10;
You must assign a value to a constant at the time you declare it. If you do not assign a value to
a constant while declaring it and try to assign a value in the execution section, you will get an error. If
you execute the below Pl/SQL block you will get error.
DECLARE
salary_increase CONSTANT number(3);
BEGIN
salary_increase := 100;
dbms_output.put_line (salary_increase);
END;

Conditional Statements in PL/SQL:


As the name implies, PL/SQL supports programming language features like conditional
statements, iterative statements. The programming constructs are similar to how you use in
programming languages like Java and C++. In this section I will provide you syntax of how to use
conditional statements in PL/SQL programming.
IF THEN ELSE STATEMENTS:
1) IF condition
THEN
statement 1;
ELSE
statement 2;
END IF;
2)IF condition 1
THEN
statement 1;
statement 2;
ELSIF condtion2 THEN
statement 3;
ELSE
statement 4;
END IF
3)
IF condition 1
THEN
statement 1;
statement 2;
ELSIF condtion2 THEN
statement 3;
ELSE
statement 4;
END IF;

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.

1.Write a PL/SQL block to find factorial of a given number


PROGRAM:
Set serveroutput on;
declare
n number;
fac number:=1;
i number;

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

dbms_output.put_line('ENTER THE MARKS');

maths:=&maths;

physics:=&physics;

chemistry:=&chemistry;
total:=(maths+physics+chemistry);

avgs:=(total/3);

if avgs<40 then

dbms_output.put_line('Grade C');

elsif avgs>40 and avgs<70 then

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

You might also like