Modifying rows in tables and views

    Available in

    DSQL, ESQL, PSQL

    Syntax

    Description

    The UPDATE statement changes values in a table or in one or more of the tables that underlie a view. The columns affected are specified in the SET clause. The rows affected may be limited by the WHERE and ROWS clauses. If neither WHERE nor ROWS is present, all the records in the table will be updated.

    6.3.1. Using an alias

    If you assign an alias to a table or a view, the alias must be used when specifying columns and also in any column references included in other clauses.

    Examples

    Correct usage:

    1. update Fruit set soort = 'pisang' where ...
    2. update Fruit set Fruit.soort = 'pisang' where ...
    3. update Fruit F set soort = 'pisang' where ...
    4. update Fruit F set F.soort = 'pisang' where ...

    Not possible:

    1. update Fruit F set Fruit.soort = 'pisang' where ...

    6.3.2. The SET Clause

    In the SET clause, the assignment phrases, containing the columns with the values to be set, are separated by commas. In an assignment phrase, column names are on the left and the values or expressions containing the assignment values are on the right. A column may be included only once in the SET clause.

    A column name can be used in expressions on the right. The old value of the column will always be used in these right-side values, even if the column was already assigned a new value earlier in the SET clause.

    Here is an example

    Data in the TSET table:

    1. A B
    2. ---
    3. 1 0
    4. 2 0

    The statement:

    1. UPDATE tset SET a = 5, b = a;

    will change the values to:

    Notice that the old values (1 and 2) are used to update the b column even after the column was assigned a new value (5).

    6.3.3. The WHERE Clause

    The clause sets the conditions that limit the set of records for a searched update.

    In PSQL, if a named cursor is being used for updating a set, using the WHERE CURRENT OF clause, the action is limited to the row where the cursor is currently positioned. This is a positioned update.

    Examples

    1. UPDATE People
    2. SET firstname = 'Boris'
    3. UPDATE employee e
    4. SET salary = salary * 1.05
    5. WHERE EXISTS(
    6. SELECT *
    7. FROM employee_project ep
    8. WHERE e.emp_no = ep.emp_no);
    9. UPDATE addresses
    10. SET city = 'Saint Petersburg', citycode = 'PET'
    11. WHERE city = 'Leningrad'
    12. UPDATE employees
    13. SET salary = 2.5 * salary
    14. WHERE title = 'CEO'

    For string literals with which the parser needs help to interpret the character set of the data, the introducer syntax may be used. The string literal is preceded by the character set name, prefixed with an underscore character:

    1. -- notice the '_' prefix
    2. UPDATE People
    3. SET name = _ISO8859_1 'Hans-Jörg Schäfer'
    4. WHERE id = 53662;
    The “Unstable Cursor” Problem

    In Firebird, up to and including Firebird 2.5, it is necessary to be aware of an implementation fault that affects updates when the WHERE conditions use the IN (<select-expr>) and the select-expr is of the form SELECT FIRST *n* or SELECT …​ ROWS. For example:

    1. UPDATE T
    2. SET ...
    3. WHERE ID IN (SELECT FIRST 1 ID FROM T);

    known affectionately as the “infinite update loop”, will continuously update rows, over and over, and give the impression that the server has hung.

    Quirks like this can affect any data-changing DML operations, most often when the selection conditions involve a subquery. Cases have been reported where sort order interferes with expectations, without involving a subquery. It happens because, in the execution layers, instead of establishing a stable “target set” and then executing the data changes to each set member, DML statements use implicit cursors for performing the operations on whatever row currently meets the conditions, without knowledge of whether that row formerly failed the condition or was updated already. Thus, using a simple example pattern:

    1. UPDATE T SET <fields> = <values>

    the execution works as:

    Firebird’s implementation does not accord with the SQL standards, which require that a stable set be established before any data are changed. Firebird 3 and higher will comply with the standard.

    6.3.4. The ORDER BY and ROWS Clauses

    If ROWS has one argument, m, the rows to be updated will be limited to the first m rows.

    Points to note

    • If m > the number of rows being processed, the entire set of rows is updated

    • If m = 0, no rows are updated

    • If m < 0, an error occurs and the update fails

    If two arguments are used, m and n, ROWS limits the rows being updated to rows from m to n inclusively. Both arguments are integers and start from 1.

    Points to note

    • If m > the number of rows being processed, no rows are updated

    • If n > the number of rows, rows from m to the end of the set are updated

    • If m < 1 or n < 1, an error occurs and the update fails

    • If n = m - 1, no rows are updated

    • If n < m -1, an error occurs and the update fails

    ROWS Example

    1. UPDATE employees
    2. SET salary = salary + 50
    3. ORDER BY salary ASC
    4. ROWS 20;

    6.3.5. The RETURNING Clause

    An UPDATE statement involving at most one row may include RETURNING in order to return some values from the row being updated. RETURNING may include data from any column of the row, not necessarily the columns that are currently being updated. It can include literals or expressions not associated with columns, if there is a need for that.

    When the RETURNING set contains data from the current row, the returned values report changes made in the BEFORE UPDATE triggers, but not those made in AFTER UPDATE triggers.

    The context variables OLD.*fieldname* and NEW.*fieldname* can be used as column names. If OLD. or NEW. is not specified, the column values returned are the NEW. ones.

    In DSQL, a statement with RETURNING always returns a single row. If the statement updates no records, the returned values contain NULL. This behaviour may change in future Firebird versions.

    The INTO Sub-clause

    In PSQL, the INTO clause can be used to pass the returning values to local variables. It is not available in DSQL. If no records are updated, nothing is returned and variables specified in RETURNING will keep their previous values.

    RETURNING Example (DSQL)
    1. UPDATE Scholars
    2. SET firstname = 'Hugh', lastname = 'Pickering'
    3. WHERE firstname = 'Henry' and lastname = 'Higgins'
    4. RETURNING id, old.lastname, new.lastname;

    6.3.6. Updating BLOB columns

    Updating a BLOB column always replaces the entire contents. Even the BLOB ID, the “handle” that is stored directly in the column, is changed. BLOBs can be updated if:

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

    2. The new value is a text string of at most 32767 bytes. Please notice: if the value is not a string literal, beware of concatenations, as these may exceed the maximum length.

    3. The source is itself a BLOB column or, more generally, an expression that returns a .

    4. You use the INSERT CURSOR statement (ESQL only).