PL-SQL Lab Programs
PL-SQL Lab Programs
No: 9
Date:
AIM:
To write a PL/SQL program to implement the concept of accepting input from the user.
ALGORITHM:
Step 1: Start the program.
OUTPUT:
SQL> /
Enter value for regno: 101
old 5: regno:=®no;
new 5: regno:=101;
Enter value for name: Ajay
old 6: name:='&name';
new 6: name:='Ajay';
Regno :101
Name :Ajay
Result:
Thus the above PL/SQL program is executed and the output is verified.
Ex.No:10
Date:
ALGORITHM:
OUTPUT:
SQL> /
Enter value for n: 5
old 6: n:=&n;
new 6: n:=5;
Factorial 120
OUTPUT:
SQL>/
Enter value for n: 6
old 6: n:=&n;
new 6: n:=6;
-*
--*
---*
----*
-----*
------*
Maximum of 3 Numbers
Using Control Structure
OUTPUT:
SQL> /
Enter value for a: 4
old 6: a:=&a;
new 6: a:=4;
Enter value for b: 3
old 7: b:=&b;
new 7: b:=3;
Enter value for c: 5
old 8: c:=&c;s
new 8: c:=5;
C is Maximum
PRIME NUMBER
declare
no number (3):= &no;
a number (4);
b number (2);
begin
for i in 2..no - 1
loop
a := no mod i;
if a = 0then
goto out;
end if;
end loop;
<<out>>
if a = 1
then
dbms_output.put_line (no || ' is a prime number');
else
dbms_output.put_line (no || ' is not a prime number');
end if;
end;
/
OUTPUT:
SQL> /
Enter value for no: 5
5 is a prime number
SQL> /
Enter value for no: 4
4 is not a prime number
OUTPUT:
SQL>/
Enter value for num: 678
the reverse number is: 876
SQL> /
Enter value for num: 1234
the reverse number is: 4321
SQL> /
Enter value for num: 345
the reverse number is: 543
SQL> Declare
2 a number;
3 s1 number default 0;
4 Begin
5 a:=1;
6 loop
7 s1:=s1+a;
8 exit when (a=100);
9 a:=a+1;
10 end loop;
11 dbms_output.put_line('Sum between 1 to 100 is '||s1);
12 End;
13 /
OUTPUT:
SQL >
Sum between 1 to 100 is 5050
Result:
Thus the above PL/SQL program is executed and the output is verified.
Ex.No: 11
Date: PROCEDURES
AIM:
To write a PL/SQL program to call a procedure.
ALGORITHM:
Step 5: Get the input from the user, find the minimum value and display the output.
Definition:-
A procedure is a subprogram that performs a specific action.
Syntax :
Procedure created.
Main program
DECLARE
a number;
b number;
c number;
BEGIN
a:= 23;
b:= 45;
findMin(a, b, c);
dbms_output.put_line(' Minimum of (23, 45) : ' || c);
END;
/
OUTPUT:
Minimum of (23,45) : 23
ALGORITHM:
Step 5: Get the input from the user, calculate the factorial by calling the function and display it to
the user.
Definition:-
A function is a subprogram that computes a value. Functions and procedure are structured
alike, except that functions have a RETURN clause.
Syntax :
FUNCTION name [ (parameter, [, parameter, ...]) ] RETURN datatype IS
[local declarations]
BEGIN
executable statements
[EXCEPTION
exception-handlers]
END [name];
PROGRAM:
FUNCTION:
SQL> create or replace function fact1(n number) return number as
fa number(10);
begin
fa:=1;
for i in 1..n
loop
fa:=fa*i;
end loop;
return fa;
end fact1;
SQL> /
Function created.
Main program
declare
n number;
f number;
begin
n:=&n;
f:=fact1(n);
dbms_output.put_line('Factorial :'||f);
end;
/
OUTPUT:
SQL> /
Enter value for n: 8
Factorial :40320
Result:
Thus the above PL/SQL program is executed and the output is verified.
Ex.No:13
Date: Exception Handling
AIM:
To write a PL/SQL program to handle exceptions.
ALGORITHM:
Syntax :
Table created.
1 row created.
SQL> /
Enter value for eno: 102
Enter value for ename: Tamil
Enter value for dept: cs
Enter value for salary: 25000
old 1: insert into emp values(&eno,'&ename','&dept',&salary)
new 1: insert into emp values(102,'Tamil','cs',25000)
1 row created.
SQL> /
Enter value for eno: 103
Enter value for ename: Ajay
Enter value for dept: IT
Enter value for salary: 27000
old 1: insert into emp values(&eno,'&ename','&dept',&salary)
new 1: insert into emp values(103,'Ajay','IT',27000)
1 row created.
SQL> /
Enter value for eno: 104
Enter value for ename: Harish
Enter value for dept:IT
Enter value for salary: 27000
old 1: insert into emp values(&eno,'&ename','&dept',&salary)
new 1: insert into emp values(104,'Harish','IT',27000)
1 row created.
SQL> /
Enter value for eno: 105
Enter value for ename: Viji
Enter value for dept: IT
Enter value for salary: 27000
old 1: insert into emp values(&eno,'&ename','&dept',&salary)
new 1: insert into emp values(105,'Viji','IT',27000)
1 row created.
CODING
SQL>declare
newsal number;
begin
select salary into newsal from emp where eno=&eno;
dbms_output.put_line(‘Newsal:’||newsal);
exception
when no_data_found then
dbms_output.put_line('no rows selected');
end;
OUTPUT:
SQL > /
Enter value for eno: 101
Newsal:25000
SQL>/
Enter value for eno: 106
no rows selected
Result:
Thus the above PL/SQL program is executed and the output is verified.