FirebirdSQL logo

Module Structure

PSQL code modules consist of a header and a body.The DDL statements for defining them are complex statements;that is, they consist of a single statement that encloses blocks of multiple statements.These statements begin with a verb (CREATE, ALTER, DROP, RECREATE, CREATE OR ALTER, or EXECUTE BLOCK) and end with the last END statement of the body.

The Module Header

The header provides the module name and defines any input and output parameters or — for functions — the return type.Stored procedures and PSQL blocks may have input and output parameters.Functions may have input parameters and must have a scalar return type.Triggers do not have either input or output parameters, but DML triggers do have the NEW and OLD “records”, and INSERTING, UPDATING and DELETING variables.

The header of a trigger indicates the DML event (insert, update or delete, or a combination) or DDL or database event and the phase of operation (BEFORE or AFTER that event) that will cause it to “fire”.

The Module Body

The module body is either a PSQL module body, or an external module body.PSQL blocks can only have a PSQL module body.

Syntax of a Module Body
<module-body> ::=
  <psql-module-body> | <external-module-body>

<psql-module-body> ::=
  AS
    [<forward-declarations>]
    [<declarations>]
  BEGIN
    [<PSQL_statements>]
  END

<external-module-body> ::=
  EXTERNAL [NAME <extname>] ENGINE engine
  [AS '<extbody>']

<forward-declarations> ::=
  <forward-declare-item> [<forward-declare-item> ...]

<declarations> ::=
  <declare-item> [<declare-item> ...]

<forward-declare-item> ::=
    <subfunc-forward>
  | <subproc-forward>

<declare-item> ::=
    <declare-var>
  | <declare-cursor>
  | <subfunc-def>
  | <subproc-def>

<extname> ::=
  '<module-name>!<routine-name>[!<misc-info>]'

<declare-var> ::=
  !! See DECLARE VARIABLE !!

<declare-cursor> ::=
  !! See DECLARE .. CURSOR !!

<subfunc-forward>, <subfunc-def> ::=
  !! See DECLARE FUNCTION !!

<subproc-forward>, <subproc-def> ::=
  !! See DECLARE PROCEDURE !!
Table 1. Module Body Parameters
Parameter Description

declarations

Section for declaring local variables, named cursors, and subroutines

PSQL_statements

Procedural SQL statements.Some PSQL statements may not be valid in all types of PSQL.For example, RETURN <value>; is only valid in functions.

subfunc-forward

Sub-function forward declaration

subproc-forward

Sub-procedure forward declaration

declare_var

Local variable declaration

declare_cursor

Named cursor declaration

subfunc-def

Sub-function declaration

subproc-def

Sub-procedure declaration

extname

String identifying the external procedure

engine

String identifying the UDR engine

extbody

External procedure body.A string literal that can be used by UDRs for various purposes.

module-name

The name of the module that contains the procedure

routine-name

The internal name of the procedure inside the external module

misc-info

Optional string that is passed to the procedure in the external module

The PSQL Module Body

The PSQL module body starts with an optional section that declares variables and subroutines, followed by a block of statements that run in a logical sequence, like a program.A block of statements — or compound statement — is enclosed by the BEGIN and END keywords, and is executed as a single unit of code.The main BEGIN…​END block may contain any number of other BEGIN…​END blocks, both embedded and sequential.Blocks can be nested to a maximum depth of 512 blocks.All statements except BEGIN and END are terminated by semicolons (‘;’).No other character is valid for use as a terminator for PSQL statements.

Switching the Terminator in isql

Here we digress a little, to explain how to switch the terminator character in the isql utility to make it possible to define PSQL modules in that environment without conflicting with isql itself, which uses the same character, semicolon (‘;’), as its own statement terminator.