You can change the values of existing rows of a table with the UPDATE statement. The basic syntax of the UPDATE statement is:
1234
UPDATE <table>
SET <column> = <value> [,
<column> = <value>, ...]
[WHERE <condition>]
You can use the UPDATE statement to change only certain rows in a table by adding a WHERE clause.
The overtime value of officials in salary group A09 should be doubled:
123
UPDATE Official
SET Overtime = Overtime * 2
WHERE Salary = 'A09';
| PNR | NAME | OVERTIME | SALARY | MANAGER |
|---|
| P01 | Mr A | 20 | A09 | P04 |
| P02 | Mr B | 10 | A10 | P04 |
| P03 | Ms C | 40 | A09 | P04 |
| P04 | Ms D | ? | A12 | P09 |
| … | … | … | … | … |
Note
The WHERE clause is optional. However, omitting the WHERE clause updates all rows of the table, which may not be the intention.
In practice, the WHERE clause often specifies the primary key values of the rows to be updated. Note that "missing rows" do not lead to an exception, they lead to no row being updated.
You can use the UPDATE statement to change values of multiple columns.
The overtime value of officials in salary group A09 should be tripled. In addition their salary group should be changed to A10.
1234
UPDATE Official
SET Overtime = Overtime * 3,
Salary = 'A10'
WHERE Salary = 'A09';
| PNR | NAME | OVERTIME | SALARY | MANAGER |
|---|
| P01 | Mr A | 30 | A10 | P04 |
| P02 | Mr B | 10 | A10 | P04 |
| P03 | Ms C | 60 | A10 | P04 |
| … | … | … | … | … |
The WHERE clause supports the same features as the SELECT statement, including subqueries. You can use an uncorrelated subquery on the WHERE clause of the UPDATE statement.
Officials who are contacts for the car owners are moved to salary group A15.
1234
UPDATE Official
SET Salary = 'A15'
WHERE PNr IN (SELECT PersNumber
FROM Contact);
| PNR | NAME | OVERTIME | SALARY | MANAGER |
|---|
| … | … | … | … | … |
| P07 | Ms G | 22 | A11 | P08 |
| P08 | Ms H | ? | A15 | P09 |
| P09 | Mr I | ? | A15 | ? |
You can also use correlated subqueries, such as functions in the WHERE clause. Usually a subquery is part of the WHERE clause, but you can also use a (correlated or uncorrelated) subquery in the SET clause.
For the following SQL statement, we assume that the table, Car, has an additional column, Ownername.
1234
UPDATE Car c
SET Ownername = (SELECT o.Name
FROM Owner o
WHERE o.OwnerID = c.Owner);
| CARID | PLATENUMBER | BRAND | COLOR | HP | OWNER | OWNERNAME |
|---|
| F01 | HD-V 106 | Fiat | red | 75 | H06 | Ms W |
| F02 | HD-VW 4711 | VW | black | 120 | H03 | SAP AG |
| F03 | HD-JA 1972 | BMW | blue | 184 | H03 | SAP AG |
| … | … | … | … | … | … | … |
In addition to the INSERT and the UPDATE statements, SAP HANA also supports an UPSERT statement. This statement either updates rows in a table or inserts new rows.
UPSERT Statement Syntax
Due to its support for inserting data, the UPSERT statement comes in three syntax variants similar to the INSERT syntax variants:
UPSERT <table>(<column List>) VALUES (<value list>) WHERE <condition>
If the condition evaluates to true, the matching rows are updated. If not, a new row with the values provided is inserted.
UPSERT <table>(<column List>) VALUES (<value list>) WITH PRIMARY KEY
Updates the row with the primary key contained in the value list if present. If not, a new row with the values provided is inserted.
UPSERT <table>(<column List>) SELECT ... FROM ... WHERE ...
For each row returned by the subquery, it checks if the table contains a row with the same primary key value. If yes, the existing row is updated with the row returned by the subquery. If not, a new row with the values provided is inserted.
The first variant looks similar to the INSERT statement, with the key word changed to UPSERT and a WHERE clause added.
The third variant looks like the INSERT statement in its SET variant and behaves much in the same way.
Alternatively, you can also use the REPLACE keyword. UPSERT and REPLACE are synonymous.
UPSERT Statements
UPSERT statements may act as either an INSERT or an UPDATE, depending on the existing data in the table.
The Employee table originally contains the rows:
| DNUMBER | NAME | DATEOFBIRTH | REMAINDERDAYS | DEPID |
|---|
| D100001 | Mr A | Jan 31, 1972 | 10 | A01 |
| D100002 | Ms B | Feb 29, 1972 | 15 | A01 |
| D100003 | Mr C | Mar 31, 1972 | 20 | A01 |
| D100004 | Ms D | Apr 30, 1972 | 10 | A02 |
| D100005 | Ms E | May 31, 1972 | 10 | A02 |
| D100006 | Mr F | Jun 30, 1972 | 40 | A03 |
| D100007 | Ms G | ? | ? | ? |
| D100008 | Mr H | ? | ? | ? |
| D100009 | Ms I | Apr 1, 1972 | 10 | A04 |
| D100010 | Ms J | Apr 30, 1972 | 0 | A04 |
| D100011 | Mr K | May 1, 1972 | 20 | A04 |
| D100012 | Mr L | May 31, 1972 | 20 | A04 |
UPSERT Values
123
UPSERT Employee
VALUES ('D100001', 'Mr X', '1975-01-31', 10, 'A01')
WHERE DNumber = 'D100001';
123
UPSERT Employee
VALUES ('D100099', 'Ms X', '1975-01-31', 10, 'A01')
WHERE DNumber = 'D100099';
| DNUMBER | NAME | DATEOFBIRTH | REMAINDERDAYS | DEPID |
|---|
| D100001 | Mr X | Jan 31, 1975 | 10 | A01 |
| D100002 | Ms B | Feb 29, 1972 | 15 | A01 |
| D100003 | Mr C | Mar 31, 1972 | 20 | A01 |
| D100004 | Ms D | Apr 30, 1972 | 10 | A02 |
| … | … | … | … | … |
| D100012 | Mr L | May 31, 1972 | 20 | A04 |
| D100099 | Ms X | Jan 31, 1975 | 10 | A01 |