Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

DECLARE TYPE

Define data types for Oracle compatibility. This statement allows declaring PL/SQL-style record types and associative arrays, and REF CURSOR types within stored procedures.

The type declarations on this page were introduced in different releases:

  • TYPE ... IS RECORD — from MariaDB 11.8.1

  • TYPE ... IS TABLE OF ... INDEX BY (associative arrays) — from MariaDB 12.0.1

  • TYPE ... IS REF CURSOR — from MariaDB 13.0.1

  • TYPE in a package specification (public, referenced by qualified name) — from MariaDB 13.1

Overview

The DECLARE TYPE declaration specifies user-defined data types that must be compatible with Oracle within stored procedures and anonymous blocks. It provides Oracle-compatible type declarations, such as record types, associative arrays, and REF CURSOR types, which allows for more flexible data handling and better compatibility with Oracle PL/SQL.

Associative arrays (INDEX BY tables) offer an in-memory key-value structure for quick data storage and effective lookups. REF CURSOR types allow for the creation of cursor variables that refer to query result sets and can be passed between program blocks.

The general syntax for defining associative array types is as follows:

DECLARE
   TYPE type_name IS TABLE OF element_type INDEX BY key_type
  • type_name is the name of the new associative array type.

  • element_type is the type of each value. It supports scalar and record types, explicit or anchored (for example, t1.col1%TYPE or t1%ROWTYPE).

  • key_type defines the key type. It must be a plain integer or string type — anchored types such as t1.col1%TYPE are not accepted here.

Associative Arrays

In Oracle, associative arrays (called index-by tables) are sparse collections of elements indexed by keys, which can be integers or strings.

Here’s an example of how to declare an associative array in Oracle:

Methods

Associative arrays support the following methods:

  • FIRST — a function that returns the first key

  • LAST — a function that returns the last key

  • NEXT — a function that returns the key after the given one

  • PRIOR — a function that returns the key before the given one

  • COUNT — a function that returns the number of elements

  • EXISTS — a function that returns TRUE if the key exists

  • DELETE — a procedure that removes a specific key, or clears the array

While the MariaDB implementation is largely aligned with Oracle’s implementation, there are a few differences:

  • Only literals as keys in the constructor: When using constructors, keys must be literals — Oracle allows expressions.

  • Collation control: Instead of NLS_SORT or NLS_COMP, MariaDB uses the SQL-standard COLLATE clause.

  • No nested associative arrays: Arrays of arrays are not supported.

These differences are largely rooted in architectural constraints — MariaDB is aiming at staying as close to Oracle semantics as possible while maintaining performance and predictability.

Examples

Associative Array of Scalar Elements

Explicit type_name

Anchored type_name

Associative Array of Records

Using Explicit Data Types

Using Anchored Data Types

RECORD Types

In sql_mode=ORACLE, the TYPE ... IS RECORD statement allows you to define a user-defined data structure consisting of one or more fields.

Before MariaDB 13.0.1, TYPE-defined RECORD types could only be used in local program blocks. Starting from MariaDB 13.0.1, RECORD can additionally be used in package routine parameters and package function RETURN clauses.

Syntax

  • record_type_name is the name of the new record type.

  • Each field_name is a named attribute of the record.

  • Each data_type is a valid MariaDB data type, or an anchored type using %TYPE (for example, t1.a%TYPE). Record fields support only scalar types, so %ROWTYPE cannot be used here.

RECORD Types in Package Routine Parameters and Function RETURN

Starting with MariaDB 13.0.1, custom types defined with DECLARE TYPE (including RECORD and REF CURSOR) can be used as:

  • Parameters of procedures and functions declared in a package

  • RETURN types of functions declared in a package

This applies to package routines only. Standalone procedures and functions cannot use a TYPE-defined type as a parameter or as a RETURN type. From MariaDB 13.1, a type declared in a package specification can be referenced by qualified name from routines outside the package — including standalone routines — subject to its own restrictions. See Package-Wide Types.

Before that release, such use was prohibited by the MariaDB grammar and resulted in:

Using RECORDs in Routine Parameters

Starting with MariaDB 13.0.1, stored routines within the same package can use a RECORD type declared inside the package body as a parameter.

Using RECORDs as a Function Return Type

A RECORD type declared inside a package body can also be used as the return type for a stored function inside the same package.

Requirement

This feature requires Oracle SQL mode at package creation time. The SQL mode is stored when the package is created, so SET sql_mode=ORACLE must be executed before running CREATE PACKAGE and CREATE PACKAGE BODY. It does not need to be set when calling the package's stored procedures or functions.

REF CURSOR Types

MariaDB supports Oracle-compatible REF CURSOR type declarations as part of the DECLARE TYPE statement. Like any other DECLARE TYPE declaration, a REF CURSOR type can be declared in a DECLARE ... BEGIN ... END block, in a stored routine, in a package body, or — from MariaDB 13.1 — in a package specification (see Package-Wide Types). The complete examples below use anonymous blocks and standalone procedures.

REF CURSOR types must be specified with a TYPE declaration before any variables of that type can be declared. It can be defined as weak or strong based on whether a return type is specified.

Syntax

  • If RETURN is omitted, the cursor type is weak.

  • If RETURN is specified, the cursor type is strong and limited to a single row structure.

Weak REF CURSOR

A weak REF CURSOR type is defined without a RETURN clause. It can be used with any query result.

Strong REF CURSOR

A strong REF CURSOR type is defined by a RETURN clause that defines the row structure the cursor must return.

Supported RETURN Types

MariaDB supports the following RETURN clause formats:

RETURN record_type

The return type can be a user-defined record type. Columns that correspond to the field names and types of the record must be returned by the query run against the cursor.

RETURN record_type%ROWTYPE

The declared RECORD type can include anchored field types, such as t1.b%TYPE. This enables the record and therefore the cursor to automatically modify in the event that the underlying table column type changes.

RETURN record_variable%TYPE

The cursor's return type is determined from a defined variable using the %TYPE attribute. At the time of declaration, the cursor inherits the type of the variable's row structure.

RETURN cursor%ROWTYPE

The row structure of an existing static cursor declared in the same block serves as the anchor for the cursor's return type. The column types and names from that cursor's SELECT statement are inherited by the REF CURSOR.

RETURN table_or_view%ROWTYPE

Using %ROWTYPE, the return type of the cursor is directly linked to a table or view row structure. This cursor variable can only be used with queries that return all columns from the referenced table or view in the same sequence.

RETURN cursor_variable%ROWTYPE

The cursor's return type is derived from another REF CURSOR variable using %ROWTYPE. This allows for the connection of cursor type declarations, so that a second cursor inherits its structure from a previously specified cursor variable.

Package-Wide Types

This feature is available from MariaDB 13.1, in sql_mode=ORACLE only.

A TYPE declaration can appear in a package specification. Such a type is public: routines outside the package can declare variables of it by qualifying the type name with the name of the package that declares it. A TYPE declaring a record, associative array, or REF CURSOR can therefore be shared between schema-level (standalone) and package routines.

For using a RECORD type as a parameter or RETURN type inside its declaring package — available from MariaDB 13.0.1, without qualified names — see RECORD Types in Package Routine Parameters and Function RETURN.

Syntax

Declare the type in the package specification:

Refer to it from elsewhere with a qualified name, in either of two forms:

Example

A stored procedure in any schema can now declare variables of these types:

Where Package Types Can Be Used

A qualified package type is accepted as:

  • The type of a local variable, in a stored procedure, a stored function, a package routine, or an anonymous block:

  • The type of a parameter of a package routine, declared either in the package specification or in the package body:

  • The RETURN type of a package function:

  • The element type of an associative array:

  • The RETURN type of a REF CURSOR type declaration:

A package specification can also use its own types by qualified name, provided the type is declared earlier in the same specification:

Referring to a type that is not yet declared, or that does not exist, fails when the package is created:

Where Package Types Cannot Be Used

Context
Result

Parameter of a schema-level (standalone) procedure or function

ERROR HY000: Incorrect usage of parameter_declaration and package_name.type_name

RETURN type of a schema-level (standalone) function

ERROR HY000: Incorrect usage of RETURN and package_name.type_name

Inside a trigger

ERROR HY000: Incorrect usage of TRIGGER and `pkg1`.`rec0_t`

Inside an event

ERROR HY000: Incorrect usage of EVENT and `pkg1`.`rec0_t`

Parameters and RETURN types of schema-level routines are exposed through INFORMATION_SCHEMA.PARAMETERS, which cannot yet represent a package type.

Name Resolution

A two-step name pkg1.rec0_t names a package rather than a schema, so the schema that holds the package is resolved through SET PATH — the same mechanism that resolves package routine calls. The default path is CURRENT_SCHEMA, so an unqualified package is looked up in the current schema. With a wider path, the same type name can resolve to different packages:

The three-step form schema_name.package_name.type_name names the schema explicitly and does not depend on the path.

Because a routine stores the resolved type, dropping the package that declares it leaves the routine unusable. The failure appears when the routine is called, not when the package is dropped:

Cyclic type dependencies between two packages are rejected: if b_pkg already refers to a type in a_pkg, then redefining a_pkg to refer back to a type in b_pkg fails with Unknown data type.

Privileges

Using a type declared in a package requires the EXECUTE privilege on that package:

Without it, creating the routine that declares the variable is refused:

The check applies to any user other than the one that created the package, including users that hold CREATE ROUTINE in the same schema. EXECUTE on a package does not imply EXECUTE on its package body, so calling the package's routines needs a separate grant.

Limitation on Dumps

mariadb-dump writes packages in name order. If two packages depend on each other's types, the file can define the dependent package first, and the restore then fails with Unknown data type.

When the packages are in different databases, dump each database separately and restore them in dependency order:

When the dependency is between packages in the same database, dump and restore the mysql.proc table instead.

See Also

This page is licensed: CC BY-SA / Gnu FDL

Last updated

Was this helpful?