Skip Headers
Oracle® TimesTen In-Memory Database SQL Reference
Release 11.2.1

Part Number E13070-04
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Master Index
Master Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

UPDATE

The UPDATE statement updates the values of one or more columns in all rows of a table or in rows that satisfy a search condition.

Required privilege

No privilege is required for the table owner.

UPDATE for another user's table.

SQL syntax

The UPDATE statement has the syntax:

UPDATE [FIRST NumRows] 
{[Owner.]TableName [CorrelationName]}
SET {ColumnName =
{Expression1 | NULL | DEFAULT}} [,...]
[ WHERE SearchCondition ]
RETURNING|RETURN Expression2[,...] INTO DataItem[,...]

Parameters

The UPDATE statement has the parameters:

Parameter Description
FIRST NumRows Specifies the number of rows to update. FIRST NumRows is not supported in subquery statements. NumRows must be either a positive INTEGER or a dynamic parameter placeholder. The syntax for a dynamic parameter placeholder is either ? or :DynamicParameter. The value of the dynamic parameter is supplied when the statement is executed.
[Owner.]TableName [CorrelationName] [Owner.]TableName identifies a table to be updated.

CorrelationName specifies a synonym for the immediately preceding table. When accessing columns of that table, use the correlation name instead of the actual table name within the statement. The correlation name must conform to the syntax rules for a basic name. See "Basic names".

All correlation names within one statement must be unique.

SET ColumnName Column to be updated. You can update several columns of the same table with a single UPDATE statement. Primary key columns can be included in the list of updated columns as long as the values of the primary key columns are not changed.
Expression1 Any expression that does not contain an aggregate function. The expression is evaluated for each row qualifying for the update operation. The data type of the expression must be compatible with the updated column's data type. Expression1 can specify a column or sequence CURRVAL or NEXTVAL reference when updating values.
NULL Puts a NULL value in the specified column of each row satisfying the WHERE clause. The column must allow NULL values.
DEFAULT Specifies that the column should be updated with the default value.
WHERE SearchCondition The search condition can contain a subquery. All rows for which the search condition is TRUE are updated as specified in the SET clause. Rows that do not satisfy the search condition are not affected. If no rows satisfy the search condition, the table is not changed.
Expression2 Valid expression syntax. See Chapter 3, "Expressions".
DataItem Host variable or PL/SQL variable that stores the retrieved Expression2 value.

Description

Examples

This example increases the price of parts costing more than $500 by 25 percent.

UPDATE purchasing.parts
SET salesprice = salesprice * 1.25
WHERE salesprice > 500.00;

This example updates the column with the NEXTVAL value from sequence seq.

UPDATE student SET studentno = seq.NEXTVAL WHERE name = 'Sally';

The following query updates the status of all the customers who have at least one unshipped order:

UPDATE customers SET customers.status = 'unshipped'
WHERE customers.id = ANY
    (SELECT orders.custid FROM orders
        WHERE orders.status = 'unshipped');

The following statement updates all the duplicate orders assuming that id is not a primary key:

UPDATE orders a
    WHERE EXISTS (SELECT 1 FROM orders b
        WHERE a.id = b.id AND a.rowid < b.rowid);

Make changes to job_id, salary and department_id for an employee whose last name is'Jones' in the employees table. Return the values for salary, last_name and department_id into variables.

Command> VARIABLE bnd1 NUMBER(8,2);
Command> VARIABLE bnd2 VARCHAR2(25) INLINE NOT NULL;
Command> VARIABLE bnd3 NUMBER(4);
Command> UPDATE employees SET job_id='SA_MAN', salary=salary+1000,
       > department_id=140 WHERE last_name='Jones'
       > RETURNING salary*0.25, last_name, department_id
       > INTO :bnd1, :bnd2, :bnd3;
1 row updated.
Command> PRINT bnd1 bnd2 bnd3;
BND1                 : 950
BND2                 : Jones
BND3                 : 140

Join update

TimesTen supports "join update" statements. A join update can be used to update one or more columns of a table using the result of a subquery.

Syntax

UPDATE [Owner.]TableName
SET ColumnName=Subquery
 [WHERE SearchCondition]

or

UPDATE [Owner.]TableName
SET (ColumnName[,…])=Subquery
 [WHERE SearchCondition]

Parameters

The UPDATE statement has the parameters:

Parameter Description
[Owner.]TableName [Owner.]TableName identifies a table to be updated.
SET (ColumnName[,...])= Subquery Column to be updated. You can update several columns of the same table with a single UPDATE statement. The SET clause can contain only one subquery, although this subquery can be nested.

The number of values in the SelectList of the subquery must be the same as the number of columns specified in the SET clause. An error is returned if the subquery returns more than one row for any updated row.

WHERE SearchCondition The search condition can contain a subquery. All rows for which the search condition is TRUE are updated as specified in the SET clause. Rows that do not satisfy the search condition are not affected. If no rows satisfy the search condition, the table is not changed.

Description

The subquery in the SET clause of a join update does not reduce the number of rows from the target table that are to be updated. The reduction must be done by specifying the WHERE clause. Thus if a row from the target table qualifies the WHERE clause but the subquery returns no rows for this row, this row is updated with NULL value in the updated column.

Examples

If a row from t1 has no match in t2, its x1 value in the first select and its x1,y1 values in the second select is set to NULL.

UPDATE t1 SET x1=(SELECT x2 FROM t2 WHERE id1=id2);
UPDATE t1 SET (x1,y1)=(SELECT x2,y2 FROM t2 WHERE id1=id2);

In order to restrict the update to update only rows from t1 that have a match in t2, a where clause with subquery has to be provided as follows:

UPDATE t1 SET x1=(SELECT x2 FROM t2 WHERE id1=id2) 
  WHERE id1 IN (SELECT id2 FROM t2);
UPDATE t1 SET (x1,y1)=(SELECT x2,y2 FROM t2 WHERE id1=id2) 
  WHERE id1 IN (SELECT id2 FROM t2);

See also

SELECT