6.2 INSERT

Used forInserting rows of data into a table

Available inDSQL, ESQL, PSQL

Syntax

  1. INSERT INTO target
  2. { DEFAULT VALUES
  3. | [(<column_list>)] [<override_opt>] <value_source> }
  4. [RETURNING <returning_list> [INTO <variables>]]
  5. <column_list> ::= col_name [, col_name ...]
  6. <override_opt> ::=
  7. OVERRIDING {USER | SYSTEM} VALUE
  8. <value_source> ::= VALUES (<value_list>) | <select_stmt>
  9. <value_list> ::= <ins_value> [, <ins_value> ...]
  10. <ins_value> :: = <value_expression> | DEFAULT
  11. <returning_list> ::= * | <output_column> [, <output_column]
  12. <output_column> ::=
  13. target.*
  14. | <return_expression> [COLLATE collation] [[AS] alias]
  15. <return_expression> ::=
  16. <value_expression>
  17. | [target.]col_name
  18. <value_expression> ::=
  19. <literal>
  20. | <context-variable>
  21. | any other expression returning a single
  22. value of a Firebird data type or NULL
  23. <variables> ::= [:]varname [, [:]varname ...]

Table 6.2.1 Arguments for the INSERT Statement Parameters

ArgumentDescription

target

The name of the table or view to which a new row, or batch of rows, should be added

col_name

Name of a table or view column

value_expression

An expression whose value is used for inserting into the table or for returning

return_expression

The expression to be returned in the RETURNING clause

literal

A literal

context-variable

Context variable

varname

Name of a PSQL local variable

The INSERT statement is used to add rows to a table or to one or more tables underlying a view:

  • If the column values are supplied in a VALUES clause, exactly one row is inserted

  • The values may be provided instead by a SELECT expression, in which case zero to many rows may be inserted

  • With the DEFAULT VALUES clause, no values are provided at all and exactly one row is inserted.

Restrictions

  • Columns returned to the NEW.*column_name* context variables in triggers should not have a colon (:) prefixed to their names

  • No column may appear more than once in the column list.

ALERT : BEFORE INSERT Triggers

Regardless of the method used for inserting rows, be mindful of any columns in the target table or view that are populated by BEFORE INSERT triggers, such as primary keys and case-insensitive search columns. Those columns should be excluded from both the column_list and the VALUES list if, as they should, the triggers test the NEW.*column_name* for NULL.

6.2.1 INSERT …​ VALUES

The VALUES list must provide a value for every column in the column list, in the same order and of the correct type. The column list need not specify every column in the target but, if the column list is absent, the engine requires a value for every column in the table or view (computed columns excluded).

The value DEFAULT allows a column to be specified in the column list, but instructs Firebird to use the default value (either NULL or the value specified in the DEFAULT clause of the column definition). For identity columns, specifying DEFAULT will generate the identity value. It is possible to include calculated columns in the column list and specifying DEFAULT as the column value.

Note

Introducer syntax provides a way to identify the character set of a value that is a string constant (literal). Introducer syntax works only with literal strings: it cannot be applied to string variables, parameters, column references or values that are expressions.

Examples

  1. INSERT INTO cars (make, model, year)
  2. VALUES ('Ford', 'T', 1908);
  3. INSERT INTO cars
  4. VALUES ('Ford', 'T', 1908, 'USA', 850);
  5. -- notice the '_' prefix (introducer syntax)
  6. INSERT INTO People
  7. VALUES (_ISO8859_1 'Hans-Jörg Schäfer');

6.2.2 INSERT …​ SELECT

For this method of inserting, the output columns of the SELECT statement must provide a value for every target column in the column list, in the same order and of the correct type.

Literal values, context variables or expressions of compatible type can be substituted for any column in the source row. In this case, a source column list and a corresponding VALUES list are required.

If the column list is absent — as it is when SELECT * is used for the source expression — the column_list must contain the names of every column in the target table or view (computed columns excluded).

Examples

  1. INSERT INTO cars (make, model, year)
  2. SELECT make, model, year
  3. FROM new_cars;
  4. INSERT INTO cars
  5. SELECT * FROM new_cars;
  6. INSERT INTO Members (number, name)
  7. SELECT number, name FROM NewMembers
  8. WHERE Accepted = 1
  9. UNION ALL
  10. SELECT number, name FROM SuspendedMembers
  11. WHERE Vindicated = 1
  12. INSERT INTO numbers(num)
  13. WITH RECURSIVE r(n) as (
  14. SELECT 1 FROM rdb$database
  15. UNION ALL
  16. SELECT n+1 FROM r WHERE n < 100
  17. )
  18. SELECT n FROM r

Of course, the column names in the source table need not be the same as those in the target table. Any type of SELECT statement is permitted, as long as its output columns exactly match the insert columns in number, order and type. Types need not be exactly the same, but they must be assignment-compatible.

Important

When using and INSERT …​ SELECT with a RETURNING clause, the SELECT has to produce at most one row, as RETURNING currently only works for statements affecting at most one row.

This behaviour may change in future Firebird versions.

6.2.3 INSERT …​ DEFAULT VALUES

The DEFAULT VALUES clause allows insertion of a record without providing any values at all, either directly or from a SELECT statement. This is only possible if every NOT NULL or CHECKed column in the table either has a valid default declared or gets such a value from a BEFORE INSERT trigger. Furthermore, triggers providing required field values must not depend on the presence of input values.

Specifying DEFAULT VALUES is equivalent to specifying a values list with value DEFAULT for all columns.

Example

  1. INSERT INTO journal
  2. DEFAULT VALUES
  3. RETURNING entry_id;

6.2.4 OVERRIDING

The OVERRIDING clause controls the behaviour of an identity column for this statement only.

OVERRIDING SYSTEM VALUE

The user-provided value for the identity column is used, and no value is generated using the identity. In other words, for this insert, the identity will behave as if it is GENERATED BY DEFAULT. This option can only be specified for tables with a GENERATED ALWAYS AS IDENTITY column.

This can be useful when merging or importing data from another source. After such an insert, it may be necessary to change the next value of the identity sequence using ALTER TABLE to prevent subsequent inserts from generating colliding identity values.

OVERRIDING USER VALUE

The user-provided value for the identity column is ignored, and the column value is generated using the identity. In other words, for this insert, the identity will behave as if it is GENERATED ALWAYS, while allowing the identity column in the column-list. This option can only be specified for tables with a GENERATED BY DEFAULT AS IDENTITY column.

It is usually simpler to leave out the identity column to achieve the same effect.

Examples of OVERRIDING

  1. -- for GENERATED ALWAYS AS IDENTITY
  2. -- value 11 is used anyway
  3. insert into objects_always (id, name)
  4. OVERRIDING SYSTEM VALUE values (11, 'Laptop');
  5. -- for GENERATED BY DEFAULT AS IDENTITY
  6. -- value 12 is not used
  7. insert into objects_default (id, name)
  8. OVERRIDING USER VALUE values (12, 'Laptop');

6.2.5 The RETURNING Clause

An INSERT statement adding at most one row may optionally include a RETURNING clause in order to return values from the inserted row. The clause, if present, need not contain all of the insert columns and may also contain other columns or expressions. The returned values reflect any changes that may have been made in BEFORE INSERT triggers.

The user executing the statement needs to have SELECT privileges on the columns specified in the RETURNING clause.

The syntax of the returning_list is similar to the column list of a SELECT clause. It is possible to reference all columns using * or *table_name*.*.

The optional INTO sub-clause is only valid in PSQL.

Multiple INSERTs

In DSQL, a statement with RETURNING always returns only one row. If the RETURNING clause is specified and more than one row is inserted by the INSERT statement, the statement fails and an error message is returned. This behaviour may change in future Firebird versions.

Examples

  1. INSERT INTO Scholars (firstname, lastname, address,
  2. phone, email)
  3. VALUES ('Henry', 'Higgins', '27A Wimpole Street',
  4. '3231212', NULL)
  5. RETURNING lastname, fullname, id;
  6. INSERT INTO Scholars (firstname, lastname, address,
  7. phone, email)
  8. VALUES (
  9. 'Henry', 'Higgins', '27A Wimpole Street',
  10. '3231212', NULL)
  11. RETURNING *;
  12. INSERT INTO Dumbbells (firstname, lastname, iq)
  13. SELECT fname, lname, iq
  14. FROM Friends
  15. ORDER BY iq ROWS 1
  16. RETURNING id, firstname, iq
  17. INTO :id, :fname, :iq;

Note

  • RETURNING is supported for VALUES and DEFAULT VALUES inserts, and singleton SELECT inserts.

  • In DSQL, a statement with a RETURNING clause always returns exactly one row. If no record was actually inserted, the fields in this row are all NULL. This behaviour may change in a later version of Firebird. In PSQL, if no row was inserted, nothing is returned, and the target variables keep their existing values.

6.2.6 Inserting into BLOB columns

Inserting into BLOB columns is only possible under the following circumstances:

  1. The client application has made special provisions for such inserts, using the Firebird API. In this case, the modus operandi is application-specific and outside the scope of this manual.

  2. The value inserted is a string literal of no more than 65,533 bytes (64KB - 3).

    Note

    A limit, in characters, is calculated at run-time for strings that are in multi-byte character sets, to avoid overrunning the bytes limit. For example, for a UTF8 string (max. 4 bytes/character), the run-time limit is likely to be about (floor(65533/4)) = 16383 characters.

  3. You are using the INSERT …​ SELECT form and one or more columns in the result set are BLOBs.