a) To print hello world message
declare
msg varchar(15):='hello world';
begin
dbms_output.put_line('message is'||msg);
end;
message ishello world
Statement processed.
b) Find given number is even or odd
DECLARE
num number(5);
rem number;
BEGIN
num:=:num;
rem:=mod(num,2);
if rem=0
then
dbms_output.put_line(' Number '||num||' is Even');
else
dbms_output.put_line(' Number '||num||' is Odd');
end if;
END;
c) Find smallest of two numbers
declare
a integer;
b integer;
s integer;
begin
a:=:a;
b:=:b;
if(a < b) then
dbms_output.put_line('a is small');
else
dbms_output.put_line('b is small');
end if;
end;
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.
e)Print the grade of the student using case stmt
DECLARE
grade char(1) := 'A';
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.
g) find factorial of a number
DECLARE
n integer;
fact integer:=1;
BEGIN
n:=:n;
while n != 0 LOOP
fact:=fact*n;
n:=n-1;
END LOOP;
dbms_output.put_line('factorial of n is '||fact);
END;
factorial of n is 120
Statement processed.
h) To write a PL/SQL block to find Sum of Digits of a given Number.
DECLARE
num number(5);
rem number(5);
sm number(5):=0;
num1 number(5);
BEGIN
num:=:num;
num1:=num;
while(num>0) loop
rem:=mod(num,10);
85
sm:=sm+rem;
num:=trunc(num/10);
end loop;
dbms_output.put_line('SUM OF DIGITS OF '||num1||' IS: '||sm);
end;
SUM OF DIGITS OF 10 IS: 1
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
j) To write a PL/SQL block to Check the Given String is Palindrome or Not
DECLARE
name1 varchar2(20);
name2 varchar2(20);
l number(5);
BEGIN
name1:=:name1;
l:=length(name1);
while l>0 loop
name2:=name2||substr(name1,l,1);
l:=l-1;
end loop;
dbms_output.put_line('REVERSE OF STRING IS:'||NAME2);
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 [Link]%type;
esal [Link]%type;
eid [Link]%type;
inc number(7,2);
begin
eid:=:empno;
select job into ejob from emp where empno=eid;
if ejob='MANAGER' then
inc:=0.2;
update emp set sal=sal+sal*inc where empno=eid;
dbms_output.put_line('manager sal updated');
elsif ejob='SALESMAN' then
inc:=0.15;
update emp set sal=sal+sal*inc where empno=eid;
dbms_output.put_line('salesman sal updated');
else
inc:=0.1;
update emp set sal=sal+sal*inc where empno=eid;
dbms_output.put_line('updated for emp who r not mgrs and salesmans');
end if;
end;
-----
o) Find out whether the commission is NULL or not for the given eid
90
declare
eid [Link] % type;
com [Link] % type;
begin
eid:=:empno;
select comm into com from emp where empno=eid;
if com is null then
dbms_output.put_line('comm is null');
else
dbms_output.put_line('comm is not null');
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:
This parameter is used for giving input to the subprograms.
It is a read-only variable inside the subprograms. Their values cannot be changed inside
the subprogram.
By default, the parameters are of IN type.
OUT Parameter:
This parameter is used 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 current subprograms.
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.
p) Write a Procedure for Fibonacci series
create or replace procedure fib(n number) as
a number:=0;
b number:=1;
c number;
i number;
92
begin
dbms_output.put_line(a);
dbms_output.put_line(b);
for i in 3..n loop
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
create or replace procedure isprime(n number) as
c integer:=0;
i integer;
begin
for i in 1..n loop
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
create or replace procedure palindrome(n number) as
rev number;
t number;
begin
rev:=0;
t:=n;
while t!=0 loop
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;
s) Write a procedure to update emp data
create or replace procedure empupdate(eno [Link]%type, inc number)as
95
begin
update emp set sal=sal+inc where empno=eno;
commit;
dbms_output.put_line('update successfully');
end;
declare
eno [Link]%type;
inc number;
begin
eno:=:eno;
inc:=:inc;
empupdate(eno,inc);
end;
select * from emp;
t) Write a procedure for swapping of two numbers
create or replace procedure swap(a in out number,b in out number) as
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