7.2Stored Procedures

A stored procedure is executable code stored in the database metadata for execution on the server. It can be called by other stored procedures (including itself), functions, triggers and client applications. A procedure that calls itself is known as recursive.

7.2.1Benefits of Stored Procedures

Stored procedures have the following advantages:

Modularity

applications working with the database can use the same stored procedure, thereby reducing the size of the application code and avoiding code duplication.

Simpler Application Support

when a stored procedure is modified, changes appear immediately to all host applications, without the need to recompile them if the parameters were unchanged.

Enhanced Performance

since stored procedures are executed on a server instead of at the client, network traffic is reduced, which improves performance.

7.2.2Types of Stored Procedures

Firebird supports two types of stored procedures: executable and selectable.

7.2.2.1Executable Procedures

Executable procedures usually modify data in a database. They can receive input parameters and return a single set of output (RETURNS) parameters. They are called using the EXECUTE PROCEDURE statement. See an example of an executable stored procedure at the end of the CREATE PROCEDURE section of Chapter 5, Data Definition (DDL) Statements.

7.2.2.2Selectable Procedures

Selectable stored procedures usually retrieve data from a database, returning an arbitrary number of rows to the caller. The caller receives the output one row at a time from a row buffer that the database engine prepares for it.

Selectable procedures can be useful for obtaining complex sets of data that are often impossible or too difficult or too slow to retrieve using regular DSQL SELECT queries. Typically, this style of procedure iterates through a looping process of extracting data, perhaps transforming it before filling the output variables (parameters) with fresh data at each iteration of the loop. A Section 7.7.14, “SUSPEND statement at the end of the iteration fills the buffer and waits for the caller to fetch the row. Execution of the next iteration of the loop begins when the buffer has been cleared.

Selectable procedures may have input parameters, and the output set is specified by the RETURNS clause in the header.

A selectable stored procedure is called with a SELECT statement. See an example of a selectable stored procedure at the end of the CREATE PROCEDURE section of Chapter 5, Data Definition (DDL) Statements.

7.2.3Creating a Stored Procedure

The syntax for creating executable stored procedures and selectable stored procedures is the same. The difference comes in the logic of the program code, specifically the absence or presence of a Section 7.7.14, “SUSPEND statement.

For information about creating stored procedures, see CREATE PROCEDURE in Chapter 5, Data Definition (DDL) Statements.

7.2.4Modifying a Stored Procedure

For information about modifying existing stored procedures, see ALTER PROCEDURE, CREATE OR ALTER PROCEDURE, RECREATE PROCEDURE.

7.2.5Dropping a Stored Procedure

For information about dropping (deleting) stored procedures, see DROP PROCEDURE.