Assignment 2 - Stored Functions
Stored functions are user defined functions, that are created using the CREATE FUNCTION
statement. The functions, thus created, are called stored functions because they are stored as
database objects within the PostgreSQL database. The CREATE FUNCTION command names
the new function, states its arguments and return type.
Creating a stored function:
Syntax:-
CREATE [OR REPLACE] FUNCTION function_name (arguments)
RETURNS return_datatype AS $variable_name$
DECLARE
declaration;
[...]
BEGIN
< function_body >
[...]
RETURN { variable_name | value }
END; LANGUAGE plpgsql;
Where,
function-name specifies the name of the function.
[OR REPLACE] option allows modifying an existing function.
The function must contain a return statement.
RETURN clause specifies that data type you are going to return from the function. The return_datatype can be a base, composite, or domain type, or can reference the type of a table column.
function-body contains the executable part.
The AS keyword is used for creating a standalone function.
plpgsql is the name of the language that the function is implemented in. Here, we use this option for PostgreSQL, it Can be SQL, C, internal, or the name of a user-defined procedural language. For backward compatibility, the name can be enclosed by single quotes.
In PostgreSQL, both stored procedures and user-defined functions are created with CREATE FUNCTION statement. There are differences between the notion of stored procedures and functions in database systems:
Stored Procedure | Function | |
Use in an expression | ![]() | ![]() |
Return a value | ![]() | ![]() |
Return values as OUT parameters | ![]() | ![]() |
Return a single result set | ![]() | ![]() |
Return multiple result sets | ![]() | ![]() |
So in most cases, the purpose of a stored procedure is to:
- Perform actions without returning any result (INSERT, UPDATE operations i.e.)
- Return one or more scalar values as OUT parameters
- Return one or more result sets
Usually the purpose of a user-defined function is to process the input parameters and return a new value.
Attributes:
PL/pgSQL provides variable attributes that basically assists or helps the database programmer , when working with database objects. These attributes are %TYPE and %ROWTYPE.
The TYPE attribute : Used to declare a variable with the data type of a referenced database
object ( a table column).
Syntax :Variable_nametable_name.column_name%TYPE ;
The %ROWTYPE attribute : Used to declare a PL/pgSQL record variable to have the same
structure as the rows in a table , that we specify in the function block.
Syntax :Variable_nametable_name%ROWTYPE;
Flow Control Satement Syntax :-
1 The IF/THEN statement
If < Condition> Then
statement;
[..]
Else
Statement;
[..]
End if;
2. Nested If-else if
If Then
Statement;
[..]
Else if Then
Statement;
[….]
Else if Then
Statement;
[……]
Else
Statement;
[….]
End if;
End if;
End if;
3.Loops
LOOP
Statement;
[………]
EXIT [label] WHEN
END LOOP;
4. While Loop
While Condition
Loop
Statement;
[…….]
End loop;
5.For Loop
For Loop–Index In {Reverse} expression1 ..... expression2
Loop
Statement;
[……..]
End loop;
6. For Loop (iterate through a query resultset)
For {record_variable | %rowtype_variable } IN
select_statement
LOOP
Statement;
[……….]
END LOOP
a) Write a function that returns the total number of customers of a particular branch.( Acceptbranch name as input parameter.)
create function s4a_a(varchar ) returns integer as $$
declare
name alias for $1;
cnt integer :=0;
temp record;
begin
for temp in select customer.cno,bname from customer,branch,b_c_loan where customer.cno=b_c_loan.cno and
branch.bid=b_c_loan.bid loop
if temp.bname=name then
cnt:=cnt+1;
end if;
end loop;
return cnt;
end;
$$ language plpgsql;
b) Write a function to find the maximum loan amount approved
create function s4a_b() returns money as $$
declare
m money :='0';
temp record;
begin
for temp in select l_amt_approved from loan_app loop
if m<temp.l_amt_approved then
m:=temp.l_amt_approved;
end if;
end loop;
return m;
end;
$$ language plpgsql;
SET B:
Using Project-Employee database
a) Write a function to accept project name as input and returns the number of employees working on the project.
b) Write a function to find the number of employees whose date of joining is before ‘03/10/2010’
create table project(pno int primary key,pname varchar(15),ptype varchar(15),duration int);
CREATE TABLE
create table emp(eno int primary key,ename varchar(15),j_date date);
CREATE TABLE
create table prog_emp(pno int references project(pno),eno int references emp(eno),s_date date);
CREATE TABLE
Input Values:-
insert into project values(1,'system','computer',100);
insert into project values(2,'robotics','computer',100);
insert into project values(3,'cloud network','computer',50);
insert into project values(4,'microprocessor','electronics',150);
insert into project values(5,'eye senser','electronics',250);
insert into project values(6,'remote car','meachinical',75);
insert into emp values(1,'anil','04-04-2010');
insert into emp values(2,'ajay','23-05-2010');
insert into emp values(3,'nadim','18-08-2011');
insert into emp values(4,'samadhan','28-12-2012');
insert into emp values(5,'mahesh','28-12-2012');
insert into prog_emp values(1,1,'04-04-2010');
insert into prog_emp values(1,2,'23-05-2010');
insert into prog_emp values(2,2,'23-05-2010');
insert into prog_emp values(3,1,'25-05-2011');
insert into prog_emp values(3,1,'25-05-2011');
insert into prog_emp values(3,4,'28-12-2012');
insert into prog_emp values(4,5,'28-12-2013');
Write a function to accept project name as input and returns the number of employees working on the project.
create or replace function ass2SetD_3(varchar ) returns integer as $$
declare
name alias for $1;
cnt integer :=0;
temp record;
begin
for temp in select emp.eno,pname from emp,prog_emp,project where emp.eno=prog_emp.eno and prog_emp.pno=project.pno loop
if temp.pname=name then
cnt:=cnt+1;
end if;
end loop;
return cnt;
end;
$$ language plpgsql;
Write a function which accepts employee name and prints the details of the project which the employee works on.
create or replace function ass2SetD_2(enm varchar) returns integer as $$
declare
temp record;
begin
for temp in select project.pno, pname,ptype,duration ,ename from project, emp, prog_emp where project.pno=prog_emp.pno and emp.eno =prog_emp.eno loop
if temp.ename=enm then
Raise Notice ' Project No : %',temp.pno;
Raise Notice ' Project Name : %',temp.pname;
Raise Notice ' Project type : %',temp.ptype;
Raise Notice ' Project Duration : %',temp.duration;
end if;
end loop;
return 1;
end;
$$ language plpgsql;
Student- Teacher database
CHAR(50))
TEACHER (TNO INTEGER, T_NAME CHAR (20), QUALIFICATION CHAR
(15),EXPERIENCE INTEGER)
The relationship is as follows: STUDENT-TEACHER: M-M with descriptive attribute SUBJECT.
CREATE TABLE
create table teacher(tno int primary key , t_name varchar(20), quali char(15), experience int);
CREATE TABLE
create table stud_teach(sno int references student(sno), tno int references teacher(tno),subject varchar(30));
CREATE TABLE
insert into student values(2,'ajay','sybcs','songaon');
insert into student values(3,'nadim','sybcs','chatori');
insert into student values(4,'samadhan','sybcs','chatori');
insert into student values(5,'mahesh','sybcs','malsakore');
insert into teacher values(1,'Borse madam','MCS,Ph.D',7);
insert into teacher values(2,'davre madam','MCS',4);
insert into teacher values(3,'Jadhav sir','Msc',3);
insert into teacher values(4,'Bidkar sir','BCS',2);
insert into teacher values(5,'Gawali madam','BCS',3);
insert into stud_teach values(1,1,'RD');
insert into stud_teach values(2,1,'SYS.PRO');
insert into stud_teach values(3,1,'RD');
insert into stud_teach values(3,2,'DS');
insert into stud_teach values(5,2,'DS');
a) Write a function to find name of the most experienced teacher for “Mathematics”.
declare
exp int;
temp record;
begin
for temp in select t_name,experience from teacher ,stud_teach where teacher.tno=stud_teach.tno and subject='math' loop
if exp<temp.experience then
exp:=temp.experience;
end if;
end loop;
return temp.t_name;
end;
$$ language plpgsql;
create or replace function as2SetC_2() returns text as $$
declare
c int;
temp record;
begin
for temp in select t_name , count(subject) as cnt from teacher,stud_teach where teacher.tno=stud_teach.tno group by t_name limit 1 loop
if c<temp.cnt then
c:=temp.cnt;
end if;
end loop;
return temp.t_name;
end;
$$ language plpgsql;
c) Write a function to find the number of teachers having qualification “Ph. D.”.
Create or Replace function as2SetC_3() returns integer as $$
declare
cnt integer :=0;
temp record;
begin
for temp in select tno,quali from teacher loop
if temp.quali='Ph.D' then
cnt:=cnt+1;
end if;
end loop;
return cnt;
end;
$$ language plpgsql;
Project – Employee Database
Employee (empno int, ename char (20), joining_date date)
The relationship between Project and Employee is many to many with descriptive attribute start_date.
CREATE TABLE
create table emp(eno int primary key,ename varchar(15),j_date date);
CREATE TABLE
create table prog_emp(pno int references project(pno),eno int references emp(eno),s_date date);
CREATE TABLE
insert into project values(1,'system','computer',100);
insert into project values(2,'robotics','computer',100);
insert into project values(3,'cloud network','computer',50);
insert into project values(4,'microprocessor','electronics',150);
insert into project values(5,'eye senser','electronics',250);
insert into project values(6,'remote car','meachinical',75);
insert into emp values(1,'anil','04-04-2010');
insert into emp values(2,'ajay','23-05-2010');
insert into emp values(3,'nadim','18-08-2011');
insert into emp values(4,'samadhan','28-12-2012');
insert into emp values(5,'mahesh','28-12-2012');
insert into prog_emp values(1,2,'23-05-2010');
insert into prog_emp values(2,2,'23-05-2010');
insert into prog_emp values(3,1,'25-05-2011');
insert into prog_emp values(3,1,'25-05-2011');
insert into prog_emp values(3,4,'28-12-2012');
insert into prog_emp values(4,5,'28-12-2013');
create or replace function ass2SetD_1(ptype varchar) returns integer as $$
declare
temp record;
begin
for temp in select pname, ptype from project loop
if temp.ptype=ptype then
Raise Notice ' Project Name : %',temp.pname;
end if;
end loop;
return 1;
end;
$$ language plpgsql;
b. Write a function which accepts employee name and prints the details of the project which the employee works on.
create or replace function ass2SetD_2(enm varchar) returns integer as $$
declare
temp record;
begin
for temp in select project.pno, pname,ptype,duration ,ename from project, emp, prog_emp where project.pno=prog_emp.pno and emp.eno =prog_emp.eno loop
if temp.ename=enm then
Raise Notice ' Project No : %',temp.pno;
Raise Notice ' Project Name : %',temp.pname;
Raise Notice ' Project type : %',temp.ptype;
Raise Notice ' Project Duration : %',temp.duration;
end if;
end loop;
return 1;
end;
$$ language plpgsql;
c. Write a function to accept project name as input and returns the number of employees working on the project.
create or replace function ass2SetD_3(varchar ) returns integer as $$
declare
name alias for $1;
cnt integer :=0;
temp record;
begin
for temp in select emp.eno,pname from emp,prog_emp,project where emp.eno=prog_emp.eno and prog_emp.pno=project.pno loop
if temp.pname=name then
cnt:=cnt+1;
end if;
end loop;
return cnt;
end;
$$ language plpgsql;
No comments:
Post a Comment