Functions and Procedures
We can store procedures in the database and execute them using a call statement.
This allows external applications to operate on the database without knowing about the internal details.
Functions/procedures can be written in SQL itself, or in an external programming language.
SQL also supports a rich set of imperative constructs, including loops, if-then-else, assignment etc.
For example: Define a function that, given the name of a department, returns the count of the number of instructors in that department:
Now, we can use the above function to find the department name and budget of all departments with more that 12 instructors:
The dept_count function could instead be written as procedure:
Procedures can be invoked either from an SQL procedure or from embedded SQL, using the call statement:
We can have more than one function/procedure with the same name (overloading), as long as the number/types of arguments is different.
Table Functions
These functions return a relation as a result.
For example: Return all instructors from a given department
Usage: select * from table (instructors_of (‘Music’))
Last updated