3.8Array Types

Note

Firebird does not offer much in the way of language or tools for working with the contents of arrays, and there are no plans to improve this. This limits the usefulness and accessibility of array types. Therefore, the general advice is: do not use arrays.

The support of arrays in the Firebird DBMS is a departure from the traditional relational model. Supporting arrays in the DBMS could make it easier to solve some data-processing tasks involving large sets of similar data.

Arrays in Firebird are stored in BLOB of a specialized type. Arrays can be one-dimensional and multi-dimensional and of any data type except BLOB and ARRAY.

Example

  |CREATE TABLE SAMPLE_ARR (
  |  ID INTEGER NOT NULL PRIMARY KEY,
  |  ARR_INT INTEGER [4]
  |);

This example will create a table with a field of the array type consisting of four integers. The subscripts of this array are from 1 to 4.

3.8.1Specifying Explicit Boundaries for Dimensions

By default, dimensions are 1-based — subscripts are numbered from 1. To specify explicit upper and lower bounds of the subscript values, use the following syntax:

  |'[' <lower>:<upper> ']'

3.8.2Adding More Dimensions

A new dimension is added using a comma in the syntax. In this example we create a table with a two-dimensional array, with the lower bound of subscripts in both dimensions starting from zero:

  |CREATE TABLE SAMPLE_ARR2 (
  |  ID INTEGER NOT NULL PRIMARY KEY,
  |  ARR_INT INTEGER [0:3, 0:3]
  |);

The database employee.fdb, found in the ../examples/empbuild directory of any Firebird distribution package, contains a sample stored procedure showing some simple work with arrays:

3.8.3PSQL Source for SHOW_LANGS, a procedure involving an array

   |CREATE OR ALTER PROCEDURE SHOW_LANGS (
   |  CODE VARCHAR(5),
   |  GRADE SMALLINT,
   |  CTY VARCHAR(15))
   |RETURNS (LANGUAGES VARCHAR(15))
   |AS
   |  DECLARE VARIABLE I INTEGER;
   |BEGIN
   |  I = 1;
   |  WHILE (I <= 5) DO
   |  BEGIN
   |    SELECT LANGUAGE_REQ[:I]
   |    FROM JOB
   |    WHERE (JOB_CODE = :CODE)
   |      AND (JOB_GRADE = :GRADE)
   |      AND (JOB_COUNTRY = :CTY)
   |      AND (LANGUAGE_REQ IS NOT NULL))
   |    INTO :LANGUAGES;
   | 
   |    IF (LANGUAGES = '') THEN
   |    /* PRINTS 'NULL' INSTEAD OF BLANKS */
   |      LANGUAGES = 'NULL';
   |    I = I +1;
   |    SUSPEND;
   |  END
   |END

If the features described are enough for your tasks, you might consider using arrays in your projects. Currently, no improvements are planned to enhance support for arrays in Firebird.