dbms-lab-manual-MR21 Syllabus Final
dbms-lab-manual-MR21 Syllabus Final
declare
msg varchar(15):='hello world';
begin
dbms_output.put_line('message is'||msg);
end;
Statement processed.
a is small
Statement processed.
d ) greatest of 3 numbers
declare
83
a integer;
b integer;
c integer;
begin
a:=:a;
b:=:b;
c:=:c;
if(a>b and a>c) then
dbms_output.put_line('a is greatest num');
elsif(b>a and b>c) then
dbms_output.put_line('b is greatest num');
else
dbms_output.put_line('c is greatest num');
end if;
end;
input:
a: 10
b: 2
c: 3
a is greatest num
Statement processed.
BEGIN
CASE grade
when 'A' then dbms_output.put_line('Excellent');
when 'B' then dbms_output.put_line('verygood');
when 'C' then dbms_output.put_line('well done');
when 'D' then dbms_output.put_line('passed');
when 'F' then dbms_output.put_line('better try again');
else dbms_output.put_line('No such grade');
END CASE;
END;
Excellent
Statement processed.
f)
--Here, sequence of statement(s) may be a single statement or a block of statements. An
--EXIT statement or an EXIT WHEN statement is required to break the loop.
--Example:
DECLARE
x number := 10;
84
BEGIN
LOOP
dbms_output.put_line(x);
x := x + 10;
exit WHEN x > 50;
END LOOP;
-- after exit, control resumes here
dbms_output.put_line('After Exit x is: ' || x);
END;
10
20
30
40
50
After Exit x is: 60
Statement processed.
factorial of n is 120
Statement processed.
Statement processed.
86
i) To write a PL/SQL block to Generate Fibonacci Series
DECLARE
num number(5);
f1 number(5):=0;
f2 number(5):=1;
f3 number(5);
i number(5):=3;
BEGIN
num:=:num;
dbms_output.put_line('THE FIBONACCI SERIES IS:');
dbms_output.put_line(f1);
dbms_output.put_line(f2);
while(i<=num) loop
f3:=f1+f2;
dbms_output.put_line(f3);
f1:=f2;
f2:=f3;
i:=i+1;
end loop;
END;
THE FIBONACCI SERIES IS:
0
1
1
2
3
5
8
13
21
34
87
if(name1=name2) then
dbms_output.put_line(name1||' IS PALINDROME ');
else
dbms_output.put_line(name1||' IS NOT PALINDROME ');
end if;
END;
REVERSE OF STRING IS:mam
mam IS PALINDROME
Statement processed.
k) MULTIPLICATION TABLE
declare
i number(2);
n number(2);
begin
n:=:n;
for i in 1..10 loop
dbms_output.put_line( n || ' * ' || i || ' = ' || n*i);
end loop;
end;
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Statement processed.
l) Print 1 to n prime numbers
declare
i integer;
n integer;
c integer;
begin
n:=:n;
for i in 2..n loop
c:=0;
for j in 1..i loop
if(mod(i,j)=0) then
c:=c+1;
end if;
end loop;
88
if (c=2) then
dbms_output.put_line(i);
end if;
end loop;
end;
2
3
5
7
Statement processed.
A GOTO statement in PL/SQL programming language provides an unconditional jump from the
GOTO to a labeled statement in the same subprogram.
Syntax
The syntax for a GOTO statement in PL/SQL is as follows −
GOTO label;
..
..
<< label >>
statement;
m) Illustrate go to statement
DECLARE
a number(2) := 1;
BEGIN
<<loopstart>>
WHILE a <= 5 LOOP
dbms_output.put_line ('value of a: ' || a);
a := a + 1;
IF a = 3 THEN
a := a + 1;
GOTO loopstart;
END IF;
END LOOP;
END;
value of a: 1
value of a: 2
value of a: 4
value of a: 5
Statement processed.
89
n) Update the sal of the emp , for manager – 20%, salesman 15%, others 10%
declare
ejob emp.job%type;
esal emp.sal%type;
eid emp.empno%type;
inc number(7,2);
begin
eid:=:empno;
if ejob='MANAGER' then
inc:=0.2;
inc:=0.15;
else
inc:=0.1;
end if;
end;
-----
o) Find out whether the commission is NULL or not for the given eid
90
declare
begin
eid:=:empno;
dbms_output.put_line('comm is null');
else
end if;
end;
PROCEDURES:
A Procedure in PL/SQL is a subprogram unit that consists of a group of PL/SQL
statements that can be called by name. Each procedure in PL/SQL has its own unique name by
which it can be referred to and called. This subprogram unit in the Oracle database is stored as
a database object.
Syntax:
CREATE OR REPLACE PROCEDURE
<procedure_name>
(
<parameterl IN/OUT <datatype>
..
.
)
[ IS | AS ]
<declaration_part>
BEGIN
<execution part>
EXCEPTION
<exception handling part>
END;
91
CREATE PROCEDURE instructs the compiler to create new procedure in Oracle.
Keyword 'OR REPLACE' instructs the compile to replace the existing procedure (if any)
with the current one.
Procedure name should be unique.
Keyword 'IS' will be used, when the stored procedure in Oracle is nested into some other
blocks. If the procedure is standalone then 'AS' will be used.
IN Parameter:
OUT Parameter:
IN OUT Parameter:
This parameter is used for both giving input and for getting output from the subprograms.
It is a read-write variable inside the subprograms. Their values can be changed inside the
subprograms.
In the calling statement, these parameters should always be a variable to hold the value
from the subprograms.
Note: RETURN statement to return the control to the calling block, but it cannot return any
values through the RETURN statement.
Procedures cannot be called directly from SELECT statements. They can be called from another
block or through EXEC keyword.
a number:=0;
b number:=1;
c number;
i number;
92
begin
dbms_output.put_line(a);
dbms_output.put_line(b);
c:=a+b;
a:=b;
b:=c;
dbms_output.put_line(c);
end loop;
end;
Procedure created.
declare
n number(10);
begin
n:=:n;
fib(n);
end;
input: 5
output:
93
Statement processed.
q) Write a procedure for finding out whether the given number is prime or not
c integer:=0;
i integer;
begin
if(mod(n,i)=0) then
c:=c+1;
end if;
end loop;
if(c=2) then
dbms_output.put_line('prime');
else
dbms_output.put_line('not prime');
end if;
end;
declare
n integer;
begin
n:=:n;
isprime(n);
end;
---
94
r) Write a procedure for finding out whether the given number is palindrome or not
rev number;
t number;
begin
rev:=0;
t:=n;
rev:=(rev*10)+mod(t,10);
t:=trunc(t/10);
end loop;
if n=rev then
dbms_output.put_line('Palindrome');
else
dbms_output.put_line('notPalindrome');
end if;
end;
declare
n number:=:n;
begin
palindrome(n);
end;
commit;
dbms_output.put_line('update successfully');
end;
declare
eno emp.empno%type;
inc number;
begin
eno:=:eno;
inc:=:inc;
empupdate(eno,inc);
end;
t number;
begin
t:=a;
a:=b;
b:=t;
end;
declare
a number:=:a;
b number:=:b;
96
begin
dbms_output.put_line('before swapping'||a||','||b);
swap(a,b);
dbms_output.put_line('after swapping'||a||','||b);
end;
97