DECLARE TYPE

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

This feature is available from MariaDB 12.1.

Overview

One of the standout features of Oracle PL/SQL is the associative array — a versatile and efficient in-memory data structure that developers rely on for fast temporary lookups, streamlined batch processing, and dynamic report generation.

DECLARE TYPE adds support for Oracle-style INDEX BY tables (associative arrays) for stored routines and anonymous blocks, using this syntax:

DECLARE
   TYPE type_name TABLE OF rec_type_name INDEX BY idx_type_name
  • type_name supports explicit and anchored data types (for instance, t1.col1%TYPE).

  • The INDEX BY clause supports integer and string data types.

  • rec_type_name supports scalar and record data types.

It supports the following associative array 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

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:

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

Last updated

Was this helpful?