Optional  The %NOFPLAN keyword specifies that InterSystems IRIS will ignore the frozen plan (if any) for this operation and generate a new query plan. The frozen plan is retained, but not used. For further details, refer to Frozen Plans in SQL Optimization Guide.

Optional  A column name or comma-separated list of column names that correspond in sequence to the supplied list of values. If omitted, the list of values is applied to all columns in column-number order.

If the specified record already exists, INSERT OR UPDATE performs an UPDATE. It updates the record with the specified field values. An update occurs even when the specified data is identical to the existing data.

INSERT OR UPDATE determines of a record exists by matching UNIQUE KEY field values to the existing data values. If a UNIQUE KEY violation occurs, INSERT OR UPDATE performs an update operation. Note that a UNIQUE KEY field value may not be a value explicitly specified in INSERT OR UPDATE; it may be the result of a column default value or a computed value.

INSERT OR UPDATE of a single record always sets the %ROWCOUNT variable to 1, and the %ROWID variable for the row that has been either inserted or updated.

INSERT OR UPDATE uses the same syntax, and generally has the same features and restrictions as the INSERT statement. Special considerations for INSERT OR UPDATE are described here. Unless otherwise stated here, refer to INSERT for details.

Privileges

INSERT OR UPDATE requires both INSERT and UPDATE privileges, as well as SELECT privilege. You must have these privileges either as table-level privileges or as column-level privileges.

IDKEY Fields

You can insert an IDKEY field value, but you cannot update an IDKEY field value. If the table has an IDKEY index and another unique key constraint, INSERT OR UPDATE matches these fields to determine whether to perform an insert or an update. If the other key constraint fails, this forces INSERT OR UPDATE to perform an update rather than an insert. However, if the specified IDKEY field values do not match the existing IDKEY field values, this update fails and generates an SQLCODE -107 error, because the update is attempting to modify the IDKEY fields.

For example, the table MyTest is defined with four fields: A, B, C, D, with IDKEY (A,B) and UNIQUE (C,D) constraints. The table contains the following records:

Row 1: A=1, B=1, C=2, D=2
Row 2: A=1, B=2, C=3, D=4

You invoke INSERT OR UPDATE ABC (A,B,C,D) VALUES (2,2,3,4) Because the UNIQUE (C,D) constraint failed, this statement cannot perform an insert. Instead, it attempts to update Row 2. The IDKEY for Row 2 is (1,2), so the INSERT OR UPDATE statement would attempt to change the field A value from 1 to 2. But you cannot change an IDKEY value, so the update fails with an SQLCODE -107 error.

%Counter Type Fields

When an INSERT OR UPDATE is executed, InterSystems IRIS initially assumes the operation will be an insert. Therefore, it increments the CounterLocation each time it is invoked. An insert uses the CounterLocation value to assign the %Counter field value. If, however, InterSystems IRIS determines that the operation needs to be an update, INSERT OR UPDATE has already incremented the CounterLocation value, but does not assign it to a %Counter field. This is shown in the following example:

The following five examples: create a new table (SQLUser.CaveDwellers); use INSERT OR UPDATE to populate this table with data, use INSERT OR UPDATE to add new rows and update existing rows; use a SELECT * to display the data; and delete the table.

The following example uses CREATE TABLE to create a table with a unique field (Num):

Run the following two examples one or more times in any order. They will insert records 1 thorough 5. If record 4 already exists, INSERT OR UPDATE will update it. Use the SELECT * example to display the table data: