UPSERTUPSERT — Either inserts new rows or updates existing rows depending on the primary key value.SyntaxUPSERT INTO table-name [( column-name [,...] )] VALUES ( value-expression [,...] )UPSERT INTO table-name [( column-name [,...] )] SELECT select-expressionDescription The UPSERT statement has the same syntax as the INSERT statement and will perform the same function,assuming a record with a matching primary key does not already exist in the database. If such a record does exist, UPSERT updates the existing record with the new column values. Note that the UPSERT statement can only be executed on tables that have a primary key. UPSERT has the same two forms as the INSERT statement: UPSERT INTO... VALUES and UPSERT INTO... SELECT. The UPSERT statement also has similar constraints and limitations as the INSERT statement with regards to joining partitioned tables and overly complex SELECT clauses. (See the description of the INSERT statement for details.) However, UPSERT INTO... SELECT has an additional limitation: the SELECT statement must produce deterministically ordered results. That is, the query must not only produce the same rows, they must be in the same order to ensure the subsequent inserts and updates produce identical results. Examples The following examples use two tables, Employee and Manager, both of which define the column emp_id as a primary key. In the first example, the UPSERT statement either creates a new row with the specified values or updates an existing row with the primary key 145303.
UPSERT INTO employee (emp_id, lastname, firstname, title, department)VALUES (145303, 'Public', 'Jane', 'Manager', 'HR');The next example copies records from the Employee table to the Manager table, if the employee's title is "Manager". Again, new records will be created or existing records updated depending on whether the employee already has a record in the Manager table. Notice the use of the primary key in an ORDER BY clause to ensure deterministic results from the SELECT statement.
UPSERT INTO Manager (emp_id, lastname, firstname, title, department)SELECT * from Employee WHERE title='Manager' ORDER BY emp_id;