3.8 Array 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

  1. CREATE TABLE SAMPLE_ARR (
  2. ID INTEGER NOT NULL PRIMARY KEY,
  3. ARR_INT INTEGER [4]
  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.1 Specifying 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:

  1. '[' <lower>:<upper> ']'

3.8.2 Adding 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:

  1. CREATE TABLE SAMPLE_ARR2 (
  2. ID INTEGER NOT NULL PRIMARY KEY,
  3. ARR_INT INTEGER [0:3, 0:3]
  4. );

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.3 PSQL Source for SHOW_LANGS, a procedure involving an array

  1. CREATE OR ALTER PROCEDURE SHOW_LANGS (
  2. CODE VARCHAR(5),
  3. GRADE SMALLINT,
  4. CTY VARCHAR(15))
  5. RETURNS (LANGUAGES VARCHAR(15))
  6. AS
  7. DECLARE VARIABLE I INTEGER;
  8. BEGIN
  9. I = 1;
  10. WHILE (I <= 5) DO
  11. BEGIN
  12. SELECT LANGUAGE_REQ[:I]
  13. FROM JOB
  14. WHERE (JOB_CODE = :CODE)
  15. AND (JOB_GRADE = :GRADE)
  16. AND (JOB_COUNTRY = :CTY)
  17. AND (LANGUAGE_REQ IS NOT NULL))
  18. INTO :LANGUAGES;
  19. IF (LANGUAGES = '') THEN
  20. /* PRINTS 'NULL' INSTEAD OF BLANKS */
  21. LANGUAGES = 'NULL';
  22. I = I +1;
  23. SUSPEND;
  24. END
  25. 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.