5.8 PROCEDURE

A stored procedure is a software module that can be called from a client, another procedure, function, executable block or trigger. Stored procedures, stored functions, executable blocks and triggers are written in procedural SQL (PSQL). Most SQL statements are available in PSQL as well, sometimes with some limitations or extensions, notable limitations are DDL and transaction control statements.

Stored procedures can have many input and output parameters.

5.8.1 CREATE PROCEDURE

Used forCreating a new stored procedure

Available inDSQL, ESQL

Syntax

  1. CREATE PROCEDURE procname [ ( [ <in_params> ] ) ]
  2. [RETURNS (<out_params>)]
  3. {<psql_procedure> | <external-module-body>}
  4. <in_params> ::= <inparam> [, <inparam> ...]
  5. <inparam> ::= <param_decl> [{= | DEFAULT} <value>]
  6. <out_params> ::= <outparam> [, <outparam> ...]
  7. <outparam> ::= <param_decl>
  8. <value> ::= {<literal> | NULL | <context_var>}
  9. <param_decl> ::= paramname <domain_or_non_array_type> [NOT NULL]
  10. [COLLATE collation]
  11. <type> ::=
  12. <datatype>
  13. | [TYPE OF] domain
  14. | TYPE OF COLUMN rel.col
  15. <domain_or_non_array_type> ::=
  16. !! See Scalar Data Types Syntax !!
  17. <psql_procedure> ::=
  18. [SQL SECURITY {INVOKER | DEFINER}]
  19. <psql-module-body>
  20. <psql-module-body> ::=
  21. !! See Syntax of Module Body !!
  22. <external-module-body> ::=
  23. !! See Syntax of Module Body !!

Table 5.8.1.1 CREATE PROCEDURE Statement Parameters

ParameterDescription

procname

Stored procedure name. The maximum length is 63 characters. Must be unique among all table, view and procedure names in the database

inparam

Input parameter description

outparam

Output parameter description

literal

A literal value that is assignment-compatible with the data type of the parameter

context_var

Any context variable whose type is compatible with the data type of the parameter

paramname

The name of an input or output parameter of the procedure. The maximum length is 63 characters. The name of the parameter must be unique among input and output parameters of the procedure and its local variables

collation

Collation sequence

The CREATE PROCEDURE statement creates a new stored procedure. The name of the procedure must be unique among the names of all stored procedures, tables and views in the database.

CREATE PROCEDURE is a compound statement, consisting of a header and a body. The header specifies the name of the procedure and declares input parameters and the output parameters, if any, that are to be returned by the procedure.

The procedure body consists of declarations for any local variables and named cursors that will be used by the procedure, followed by one or more statements, or blocks of statements, all enclosed in an outer block that begins with the keyword BEGIN and ends with the keyword END. Declarations and embedded statements are terminated with semi-colons (;).

5.8.1.1 Statement Terminators

Some SQL statement editors — specifically the isql utility that comes with Firebird and possibly some third-party editors — employ an internal convention that requires all statements to be terminated with a semi-colon. This creates a conflict with PSQL syntax when coding in these environments. If you are unacquainted with this problem and its solution, please study the details in the PSQL chapter in the section entitled Switching the Terminator in isql.

5.8.1.2 Parameters

Each parameter has a data type. The NOT NULL constraint can also be specified for any parameter, to prevent NULL being passed or assigned to it.

A collation sequence can be specified for string-type parameters, using the COLLATE clause.

Input Parameters

Input parameters are presented as a parenthesized list following the name of the function. They are passed by value into the procedure, so any changes inside the procedure has no effect on the parameters in the caller. Input parameters may have default values. Parameters with default values specified must be added at the end of the list of parameters.

Output Parameters

The optional RETURNS clause is for specifying a parenthesised list of output parameters for the stored procedure.

5.8.1.3 SQL Security

The SQL SECURITY clause specifies the security context for executing other routines or inserting into other tables. When SQL Security is not specified, the default value of the database is applied at runtime.

The SQL SECURITY clause can only be specified for PSQL procedures, and is not valid for procedures defined in a package.

See also SQL Security in chapter Security.

5.8.1.4 Variable, Cursor and Subroutine Declarations

The optional declarations section, located at the start of the body of the procedure definition, defines variables (including cursors) and subroutines local to the procedure. Local variable declarations follow the same rules as parameters regarding specification of the data type. See details in the PSQL chapter for DECLARE VARIABLE, DECLARE CURSOR, DECLARE FUNCTION, and DECLARE PROCEDURE.

5.8.1.5 External UDR Procedures

A stored procedure can also be located in an external module. In this case, instead of a procedure body, the CREATE PROCEDURE specifies the location of the procedure in the external module using the EXTERNAL clause. The optional NAME clause specifies the name of the external module, the name of the procedure inside the module, and — optionally — user-defined information. The required ENGINE clause specifies the name of the UDR engine that handles communication between Firebird and the external module. The optional AS clause accepts a string literal body, which can be used by the engine or module for various purposes.

5.8.1.6 Who Can Create a Procedure

The CREATE PROCEDURE statement can be executed by:

The user executing the CREATE PROCEDURE statement becomes the owner of the table.

5.8.1.7 Examples

  1. Creating a stored procedure that inserts a record into the BREED table and returns the code of the inserted record:

    1. CREATE PROCEDURE ADD_BREED (
    2. NAME D_BREEDNAME, /* Domain attributes are inherited */
    3. NAME_EN TYPE OF D_BREEDNAME, /* Only the domain type is inherited */
    4. SHORTNAME TYPE OF COLUMN BREED.SHORTNAME,
    5. /* The table column type is inherited */
    6. REMARK VARCHAR(120) CHARACTER SET WIN1251 COLLATE PXW_CYRL,
    7. CODE_ANIMAL INT NOT NULL DEFAULT 1
    8. )
    9. RETURNS (
    10. CODE_BREED INT
    11. )
    12. AS
    13. BEGIN
    14. INSERT INTO BREED (
    15. CODE_ANIMAL, NAME, NAME_EN, SHORTNAME, REMARK)
    16. VALUES (
    17. :CODE_ANIMAL, :NAME, :NAME_EN, :SHORTNAME, :REMARK)
    18. RETURNING CODE_BREED INTO CODE_BREED;
    19. END
  2. Creating a selectable stored procedure that generates data for mailing labels (from employee.fdb):

    1. CREATE PROCEDURE mail_label (cust_no INTEGER)
    2. RETURNS (line1 CHAR(40), line2 CHAR(40), line3 CHAR(40),
    3. line4 CHAR(40), line5 CHAR(40), line6 CHAR(40))
    4. AS
    5. DECLARE VARIABLE customer VARCHAR(25);
    6. DECLARE VARIABLE first_name VARCHAR(15);
    7. DECLARE VARIABLE last_name VARCHAR(20);
    8. DECLARE VARIABLE addr1 VARCHAR(30);
    9. DECLARE VARIABLE addr2 VARCHAR(30);
    10. DECLARE VARIABLE city VARCHAR(25);
    11. DECLARE VARIABLE state VARCHAR(15);
    12. DECLARE VARIABLE country VARCHAR(15);
    13. DECLARE VARIABLE postcode VARCHAR(12);
    14. DECLARE VARIABLE cnt INTEGER;
    15. BEGIN
    16. line1 = '';
    17. line2 = '';
    18. line3 = '';
    19. line4 = '';
    20. line5 = '';
    21. line6 = '';
    22. SELECT customer, contact_first, contact_last, address_line1,
    23. address_line2, city, state_province, country, postal_code
    24. FROM CUSTOMER
    25. WHERE cust_no = :cust_no
    26. INTO :customer, :first_name, :last_name, :addr1, :addr2,
    27. :city, :state, :country, :postcode;
    28. IF (customer IS NOT NULL) THEN
    29. line1 = customer;
    30. IF (first_name IS NOT NULL) THEN
    31. line2 = first_name || ' ' || last_name;
    32. ELSE
    33. line2 = last_name;
    34. IF (addr1 IS NOT NULL) THEN
    35. line3 = addr1;
    36. IF (addr2 IS NOT NULL) THEN
    37. line4 = addr2;
    38. IF (country = 'USA') THEN
    39. BEGIN
    40. IF (city IS NOT NULL) THEN
    41. line5 = city || ', ' || state || ' ' || postcode;
    42. ELSE
    43. line5 = state || ' ' || postcode;
    44. END
    45. ELSE
    46. BEGIN
    47. IF (city IS NOT NULL) THEN
    48. line5 = city || ', ' || state;
    49. ELSE
    50. line5 = state;
    51. line6 = country || ' ' || postcode;
    52. END
    53. SUSPEND; -- the statement that sends an output row to the buffer
    54. -- and makes the procedure "selectable"
    55. END
  3. With DEFINER set for procedure p, user US needs only the EXECUTE privilege on p. If it were set for INVOKER, either the user or the procedure would also need the INSERT privilege on table t.

    1. set term ^;
    2. create procedure p (i integer) SQL SECURITY DEFINER
    3. as
    4. begin
    5. insert into t values (:i);
    6. end^
    7. set term ;^
    8. grant execute on procedure p to user us;
    9. commit;
    10. connect 'localhost:/tmp/17.fdb' user us password 'pas';
    11. execute procedure p(1);

See alsoSection 5.8.3, CREATE OR ALTER PROCEDURE, Section 5.8.2, ALTER PROCEDURE, Section 5.8.5, RECREATE PROCEDURE, Section 5.8.4, DROP PROCEDURE

5.8.2 ALTER PROCEDURE

Used forModifying an existing stored procedure

Available inDSQL, ESQL

Syntax

  1. ALTER PROCEDURE procname [ ( [ <in_params> ] ) ]
  2. [RETURNS (<out_params>)]
  3. {<psql_procedure> | <external-module-body>}
  4. !! See syntax of CREATE PROCEDURE for further rules !!

The ALTER PROCEDURE statement allows the following changes to a stored procedure definition:

  • the set and characteristics of input and output parameters

  • local variables

  • code in the body of the stored procedure

After ALTER PROCEDURE executes, existing privileges remain intact and dependencies are not affected.

Altering a procedure without specifying the SQL SECURITY clause will remove the SQL Security property if currently set for this procedure. This means the behaviour will revert to the database default.

Caution

Take care about changing the number and type of input and output parameters in stored procedures. Existing application code and procedures and triggers that call it could become invalid because the new description of the parameters is incompatible with the old calling format. For information on how to troubleshoot such a situation, see the article The RDB$VALID_BLR Field in the Appendix.

5.8.2.1 Who Can Alter a Procedure

The ALTER PROCEDURE statement can be executed by:

  • Administrators

  • The owner of the stored procedure

  • Users with the ALTER ANY PROCEDURE privilege

5.8.2.2 ALTER PROCEDURE Example

Altering the GET_EMP_PROJ stored procedure.

  1. ALTER PROCEDURE GET_EMP_PROJ (
  2. EMP_NO SMALLINT)
  3. RETURNS (
  4. PROJ_ID VARCHAR(20))
  5. AS
  6. BEGIN
  7. FOR SELECT
  8. PROJ_ID
  9. FROM
  10. EMPLOYEE_PROJECT
  11. WHERE
  12. EMP_NO = :emp_no
  13. INTO :proj_id
  14. DO
  15. SUSPEND;
  16. END

See alsoSection 5.8.1, CREATE PROCEDURE, Section 5.8.3, CREATE OR ALTER PROCEDURE, Section 5.8.5, RECREATE PROCEDURE, Section 5.8.4, DROP PROCEDURE

5.8.3 CREATE OR ALTER PROCEDURE

Used forCreating a new stored procedure or altering an existing one

Available inDSQL

Syntax

  1. CREATE OR ALTER PROCEDURE procname [ ( [ <in_params> ] ) ]
  2. [RETURNS (<out_params>)]
  3. {<psql_procedure> | <external-module-body>}
  4. !! See syntax of CREATE PROCEDURE for further rules !!

The CREATE OR ALTER PROCEDURE statement creates a new stored procedure or alters an existing one. If the stored procedure does not exist, it will be created by invoking a CREATE PROCEDURE statement transparently. If the procedure already exists, it will be altered and compiled without affecting its existing privileges and dependencies.

5.8.3.1 CREATE OR ALTER PROCEDURE Example

Creating or altering the GET_EMP_PROJ procedure.

  1. CREATE OR ALTER PROCEDURE GET_EMP_PROJ (
  2. EMP_NO SMALLINT)
  3. RETURNS (
  4. PROJ_ID VARCHAR(20))
  5. AS
  6. BEGIN
  7. FOR SELECT
  8. PROJ_ID
  9. FROM
  10. EMPLOYEE_PROJECT
  11. WHERE
  12. EMP_NO = :emp_no
  13. INTO :proj_id
  14. DO
  15. SUSPEND;
  16. END

See alsoSection 5.8.1, CREATE PROCEDURE, Section 5.8.2, ALTER PROCEDURE, Section 5.8.5, RECREATE PROCEDURE

5.8.4 DROP PROCEDURE

Used forDeleting a stored procedure

Available inDSQL, ESQL

Syntax

  1. DROP PROCEDURE procname

Table 5.8.4.1 DROP PROCEDURE Statement Parameter

ParameterDescription

procname

Name of an existing stored procedure

The DROP PROCEDURE statement deletes an existing stored procedure. If the stored procedure has any dependencies, the attempt to delete it will fail and the appropriate error will be raised.

5.8.4.1 Who Can Drop a Procedure

The ALTER PROCEDURE statement can be executed by:

  • Administrators

  • The owner of the stored procedure

  • Users with the DROP ANY PROCEDURE privilege

5.8.4.2 DROP PROCEDURE Example

Deleting the GET_EMP_PROJ stored procedure.

  1. DROP PROCEDURE GET_EMP_PROJ;

See alsoSection 5.8.1, CREATE PROCEDURE, Section 5.8.5, RECREATE PROCEDURE

5.8.5 RECREATE PROCEDURE

Used forCreating a new stored procedure or recreating an existing one

Available inDSQL

Syntax

  1. RECREATE PROCEDURE procname [ ( [ <in_params> ] ) ]
  2. [RETURNS (<out_params>)]
  3. {<psql_procedure> | <external-module-body>}
  4. !! See syntax of CREATE PROCEDURE for further rules !!

The RECREATE PROCEDURE statement creates a new stored procedure or recreates an existing one. If there is a procedure with this name already, the engine will try to delete it and create a new one. Recreating an existing procedure will fail at the COMMIT request if the procedure has dependencies.

Warning

Be aware that dependency errors are not detected until the COMMIT phase of this operation.

After a procedure is successfully recreated, privileges to execute the stored procedure, and the privileges of the stored procedure itself are dropped.

5.8.5.1 RECREATE PROCEDURE Example

Creating the new GET_EMP_PROJ stored procedure or recreating the existing GET_EMP_PROJ stored procedure.

  1. RECREATE PROCEDURE GET_EMP_PROJ (
  2. EMP_NO SMALLINT)
  3. RETURNS (
  4. PROJ_ID VARCHAR(20))
  5. AS
  6. BEGIN
  7. FOR SELECT
  8. PROJ_ID
  9. FROM
  10. EMPLOYEE_PROJECT
  11. WHERE
  12. EMP_NO = :emp_no
  13. INTO :proj_id
  14. DO
  15. SUSPEND;
  16. END

See alsoSection 5.8.1, CREATE PROCEDURE, Section 5.8.4, DROP PROCEDURE, Section 5.8.3, CREATE OR ALTER PROCEDURE