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.

This feature is available from MariaDB 12.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 TABLE OF rec_type_name INDEX BY idx_type_name
  • type_name supports explicit and anchored data types (for instance, t1.col1%TYPE).

  • rec_type_name supports scalar and record data types.

  • The INDEX BY clause defines the key type using idx_type_name, which supports integer and string data types.

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:

DECLARE
  TYPE array_t IS TABLE OF VARCHAR2(64) INDEX BY PLS_INTEGER;
  array array_t;
BEGIN
  array(1) := 'Hello';
  array(2) := 'World';
  DBMS_OUTPUT.PUT_LINE(array(1));
END;

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.

Previously, TYPE-defined RECORD types could only be used in local program blocks and not in routine parameters or 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 field_type is a valid MariaDB data type or anchored type (%TYPE, %ROWTYPE).

RECORD Types in 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 stored procedures and functions

  • RETURN types of stored functions

Earlier, the use of a RECORD type in these contexts was prohibited by the MariaDB grammar and resulted in:

This change improves compatibility with Oracle PL/SQL, especially for private helper routines inside packages and for REF CURSOR usage.

Using RECORDs in Routine Parameters

Starting with MariaDB 13.0.1, stored procedures 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 a part of the DECLARE TYPE statement. A REF CURSOR is a cursor variable that can refer to a query result set and be passed across program blocks, such as stored procedures or functions.

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.

See Also

Last updated

Was this helpful?