UPDATE

UPDATE#

Changes the values of existing rows in a table or view.

Syntax#

update ::=

UPDATE TOP ( n ) table SET column = expression , fromClausefromClause whereClausewhereClause optionClauseoptionClause

Click an element in the diagram to drill down to its definition.

Arguments#

TOP ( n )

Limits the statement to updating n rows. The rows affected are arbitrary unless the order is otherwise constrained.

table

The target table or view whose rows are updated.

SET column = expression

A comma-separated list of assignments. Each names a column and the expression that becomes its new value.

FROM

An optional FROM clause used to derive the new values by joining the target with other tables or views.

WHERE

A WHERE search condition that restricts which rows are updated. If it is omitted, every row is updated.

OPTION

Query hints for the statement; see the OPTION clause.

Examples#

Update a single column for selected rows:

UPDATE dbo.Product
SET    Price = Price * 1.10
WHERE  CategoryId = 5;

Update several columns at once:

UPDATE dbo.Product
SET    Price = 19.99,
       Discontinued = 0
WHERE  Id = 42;

Derive new values from another table with FROM:

UPDATE p
SET    p.Price = s.NewPrice
FROM   dbo.Product p
JOIN   dbo.PriceStaging s ON s.ProductId = p.Id;

See also