DECLARE TYPE
Define data types for Oracle compatibility. This statement allows declaring PL/SQL-style record types and associative arrays within stored procedures.
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_nametype_namesupports explicit and anchored data types (for instance,t1.col1%TYPE).The
INDEX BYclause supports integer and string data types.rec_type_namesupports scalar and record data types.
It supports the following associative array methods:
FIRST— a function that returns the first keyLAST— a function that returns the last keyNEXT— a function that returns the key after the given onePRIOR— a function that returns the key before the given oneCOUNT— a function that returns the number of elementsEXISTS— a function that returnsTRUEif the key existsDELETE— 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_SORTorNLS_COMP, MariaDB uses the SQL-standardCOLLATEclause.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?

