Procedures, also called stored procedures, are database
objects created by executing the CREATE PROCEDURE statement. Procedures
can encapsulate logic and SQL statement and can serve as sub-routine
extensions to client applications, routines, triggers, and dynamic
compound statements.
Procedures are invoked by executing the CALL statement with a reference
to a procedure. Procedures can take input, output, and input-output
parameters, execute a wide variety of SQL statements, and return multiple
result sets to the caller.
- Features
-
- Enable the encapsulation of logic elements and SQL statements
that formulate a particular subroutine module
- Can be called from client applications, other routines, triggers,
and dynamic compound statements - from anywhere that the CALL statement
can be executed.
- Return multiple result-sets
- Support the execution of a large set of SQL statements including
SQL statements that read or modify table data in both single and multiple
partition databases
- Parameter support for input, output, and input-output parameters
- Nested procedure calls and function invocations are supported
- Recursive calls to procedures are supported
- Savepoints and transaction control are supported within procedures
- Limitations
-
- Procedures cannot be invoked from within SQL statements other
than the CALL statement. As an alternative, functions can be used
to express logic that transforms column values.
- Output parameter values and result sets of procedure calls cannot
be directly used by another SQL statement. Application logic must
be used to assign these to variables that can be used in subsequent
SQL statements.
- Procedures cannot preserve state between invocations.
- Common uses
-
- Standardization of application logic
- If multiple applications must similarly access or modify the database,
a procedure can provide a single interface for the logic. The procedure
is then available for re-use. Should the interface need to change
to accommodate a change in business logic, only the single procedure
must be modified.
- Isolation of database operations from non-database logic within
applications
- Procedures facilitate the implementation of sub-routines that
encapsulate the logic and database accesses associated with a particular
task that can be reused in multiple instances. For example, an employee
management application can encapsulate the database operations specific
to the task of hiring an employee. Such a procedure might insert employee
information into multiple tables, calculate the employee's weekly
pay based on an input parameter, and return the weekly pay value as
an output parameter. Another procedure could do statistical analysis
of data in a table and return result sets that contain the results
of the analysis.
- Simplification of the management of privileges for a group of
SQL statements
- By allowing a grouping of multiple SQL statements to be encapsulated
into one named database object, procedures allow database administrators
to manage fewer privileges. Instead of having to grant the privileges
required to execute each of the SQL statements in the routine, they
must only manage the privilege to invoke the routine.
- Supported implementations
-
- There are built-in procedures that are ready-to-use, or
users can create user-defined procedures. The following user-defined
implementations are supported for procedures:
- SQL implementation
- External implementation