FirebirdSQL logo

A package is a group of procedures and functions managed as one entity.

CREATE PACKAGE

Creates a package header

Available in

DSQL

Syntax
CREATE PACKAGE package_name
[SQL SECURITY {INVOKER | DEFINER}]
AS
BEGIN
  [ <package_item> ... ]
END

<package_item> ::=
    <function_decl>;
  | <procedure_decl>;

<function_decl> ::=
  FUNCTION funcname [ ( [ <in_params> ] ) ]
  RETURNS <domain_or_non_array_type> [COLLATE collation]
  [DETERMINISTIC]

<procedure_decl> ::=
  PROCEDURE procname [ ( [ <in_params> ] ) ]
  [RETURNS (<out_params>)]

<in_params> ::= <inparam> [, <inparam> ... ]

<inparam> ::= <param_decl> [ { = | DEFAULT } <value> ]

<out_params> ::= <outparam> [, <outparam> ...]

<outparam> ::= <param_decl>

<value> ::= { literal | NULL | context_var }

<param-decl> ::= paramname <domain_or_non_array_type> [NOT NULL]
  [COLLATE collation]

<domain_or_non_array_type> ::=
  !! See Scalar Data Types Syntax !!
Table 1. CREATE PACKAGE Statement Parameters
Parameter Description

package_name

Package name.The maximum length is 63 characters.The package name must be unique among all package names.

function_decl

Function declaration

procedure_decl

Procedure declaration

func_name

Function name.The maximum length is 63 characters.The function name must be unique within the package.

proc_name

Procedure name.The maximum length is 63 characters.The function name must be unique within the package.

collation

Collation

inparam

Input parameter declaration

outparam

Output parameter declaration

literal

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

context_var

Any context variable that is assignment-compatible with the data type of the parameter

paramname

The name of an input parameter of a procedure or function, or an output parameter of a procedure.The maximum length is 63 characters.The name of the parameter must be unique among input and output parameters of the procedure or function.

The CREATE PACKAGE statement creates a new package header.Routines (procedures and functions) declared in the package header are available outside the package using the full identifier (package_name.proc_name or package_name.func_name).Routines defined only in the package body — but not in the package header — are not visible outside the package.

Note
Package procedure and function names may shadow global routines

If a package header or package body declares a procedure or function with the same name as a stored procedure or function in the global namespace, it is not possible to call that global procedure or function from the package body.In this case, the procedure or function of the package will always be called.

For this reason, it is recommended that the names of stored procedures and functions in packages do not overlap with names of stored procedures and functions in the global namespace.

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 semicolon.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.

SQL Security

The SQL SECURITY clause specifies the security context for executing other routines or inserting into other tables from functions or procedures defined in this package.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 the package, not for individual procedures and functions of the package.

See also SQL Security in chapter Security.