Stored PL/pgSQL procedures and functions allows fro extensibility of the existing implementation of SQL.
Assume that we would like to implement the following query:
"Find the names of all departments together with a list of courses offered by each department"
and we would like to display the results in a form where a name of a department is associated with a list of titles of offered courses.
DEPARTMENT NAME | LIST OF COURSES OFFERED
--------------------------------------------------------
Math Calculus Topology Logic Algebra
Comp Sci Python Java Databases
Biol
Phys Relativity Mechanics
Astro Astrology
Implementation of a special non-tabular formatting with one dynamically extensible column is not a simple task in SQL. SELECT statement of SQL transforms data in tabular 1NF format into data in tabular 1NF format.
To create a list of courses we implement a function LCOURSES( dept_name ) that returns a list of courses offered by a department whose name is a value of a parameter dept_name. A sample implementation of such function is presented in one of the coming slides.
A function LCOURSES can be applied as an ordinary row function. It returns a string with the titles of courses offered by a department passed through a parameters. If it is a row function then it can be used like any other row function included in a library of standard functions available for a given implementation of SQL. A row function is called for each row processed by SELECT statement.
We can use a function LCOURSES as a row function in SELECT statement in the following way.
SELECT dname, LCOURSES( dname )
FROM DEPARTMENT;
A function LCOURSES is called for every row retrieved from a relational table DEPARTMENT like any standard row function, for example as UPPER function.