Professional Documents
Culture Documents
Date : 27-03-21
PROCEDURES AND FUNCTIONS
PROCEDURE:
1A.WITHOUT PARAMETER
PROGRAM :
set serveroutput.on
OUTPUT :
Procedure created.
value of c is3
Statement processed.
1B.WITHOUT PARAMETER
PROGRAM :
set serveroutput.on
OUTPUT :
Procedure created.
divide by zero
Statement processed.
1C.WITH IN AND OUT PARAMETERS:
PROGRAM :
FACT1.SQL
create or replace procedure fact1(n in number,f out number)is
i number;
begin
f:=1;
for i in 2..n loop
f:=f*i;
end loop;
end;
/
set serveroutput.on
Procedure created.
FACTORIAL.SQL
declare
f number;
n number:=5;
begin
fact1(n,f);
dbms_output.put_line('factorial of '|| n ||' is:'||f);
end;
/
set serveroutput.on
OUTPUT :
factorial of 5 is:120
Statement processed.
1D. WITH IN OUT PARAMETERS:
PROGRAM :
BEFORE.SQL
create or replace procedure multn(n in out int)is
begin
n:=n*3;
end;
/
set serveroutput.on
Procedure created.
AFTER.SQL
declare
r int;
begin
r:=7;
dbms_output.put_line('before call r is:'||r);
multn(r);
dbms_output.put_line('after call r is:'||r);
end;
/
set serveroutput.on
OUTPUT :
before call r is:7
after call r is:21
Statement processed.
FUNCTIONS:
FACT.SQL
create or replace function factorial(a number)
return number is
f number:=1;
i integer;
begin
for i in 1..a loop
f:=f*i;
end loop;
return f;
end factorial;
MAINFACT.SQL
declare
n number;
c number;
begin
n:=&n;
c:=factorial(n);
dbms_output.put_line('Factorial:'||c);
end;
/
set serveroutput on;
OUTPUT :
Factorial:120
Statement processed.
PROGRAM TO DISPLAY HOW MANY EMPLOYEES IN THE EMPLOYEE
DATABASE
EMPL.SQL
create or replace function empfn return number is
total number(2):=0;
begin
select count(*) into total from emp;
return total;
end;
/
set serveroutput.on
Function created.
EMPLMAIN.SQL
declare
c number(2);
begin
c:=empfn();
dbms_output.put_line('total employees='||c);
end;
/
set serveroutput.on
OUTPUT :
total employees=1
Statement processed.
PROGRAM TO DISPLAY THE SALARY OF THE PARTICULAR EMPLOYEE USING
FUNCTIONS
SAL.SQL
create table customers(id number,name varchar2(30),salary number);
insert into customers values(101,'nivedha',30000);
insert into customers values(102,'divya',30000);
create or replace function empfn return number is
sal1 number;
id1 number;
begin
id1:=102;
select salary into sal1 from customers where id=id1;
return sal1;
end;
/
set serveroutput.on
Function created.
SALMAIN .SQL
declare
c number;
begin
c:=empfn();
dbms_output.put_line('salary of the employees:'||c);
end;
/
OUTPUT :
salary of the employees:30000
Statement processed.