| Lesson 2 | What is a function? |
| Objective | Uses of Oracle function/identify correct syntax. |
List the uses of a function and identify correct syntax.
A function is a set of PL/SQL code that is stored with a name inside the database. Like other objects created in the database, a function is created and owned by an Oracle user. Once created, it can be executed the same way that Oracle's built-in functions are executed: by calling it by name within a query or some other SQL or PL/SQL statement. A function usually accepts one or more parameters and always returns a value.
Advantages of functionsA function is a set of PL/SQL code that is stored with a name inside the database. Like other objects created in the database, a function is created and owned by an Oracle user. Once created, it can be executed the same way that Oracle's built-in functions are executed: by calling it by name within a query or some other SQL or PL/SQL statement. A function usually accepts one or more parameters and always returns a value.
A stored function
is one that is stored as a named object in the database. One advantage of using a stored function is that the function is executed within the
server as opposed to executed within the client. The function can take advantage of higher processing power and of local access to additional
data within the server.
Advantage of Functions
Functions can be defined independently within a PL/SQL block without being stored inside the database. These kinds of functions are used in
applications and might be executed on the client side. The stored functions we are discussing in this module, however, are much more common--they
are stored within the database itself, which makes them reusable and shareable.
Uses of Oracle functionsFunctions are commonly used to:
Stored function restrictions
- Perform complex calculations and return the results
- Accept an unformatted data field and return it in a standard format, such as a Social Security Number
- Accept a key field and return summarized data from related tables
There are some restrictions in using and coding stored functions.
The following table shows the capabilities and restrictions of stored functions.
The following table shows the capabilities and restrictions of stored functions.
| Capability of stored function | Restriction of stored function |
| Can be used within SQL commands (such as queries) and within a PL/SQL block so long as the function returns a standard Oracle datatype (number, date, varchar2, char) | Cannot modify data directly, so they cannot include INSERT, UPDATE, or DELETE commands |
| Can only be used within a PL/SQL block if the function returns a Boolean datatype, or other PL/SQL datatype (other than the standard Oracle datatypes listed above) | Cannot be used inside a CHECK constraint |
The next lesson explains the proper syntax for creating a stored function.