MSDN Documentation

SQL Server Documentation

SQL UPDATE Statement

The UPDATE statement is used to modify existing records in a table. It allows you to change the values of one or more columns in rows that meet specific criteria.

Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Parameters

Examples

Example 1: Update a single column for a specific record

This example updates the Email of a customer with CustomerID 10.

UPDATE Customers
SET Email = 'new.email@example.com'
WHERE CustomerID = 10;

Example 2: Update multiple columns for multiple records

This example increases the DiscountPercentage by 5% for all customers in 'USA' and updates their ModifiedDate.

UPDATE Customers
SET DiscountPercentage = DiscountPercentage + 0.05,
    ModifiedDate = GETDATE()
WHERE Country = 'USA';

Example 3: Update a column based on another table (using a subquery)

This example updates the StockQuantity in the Products table based on the total quantity ordered in the OrderDetails table.

UPDATE Products
SET StockQuantity = StockQuantity - (
    SELECT SUM(Quantity)
    FROM OrderDetails
    WHERE OrderDetails.ProductID = Products.ProductID
)
WHERE EXISTS (
    SELECT 1
    FROM OrderDetails
    WHERE OrderDetails.ProductID = Products.ProductID
);

Important Considerations

Related Topics

SQL SELECT Statement
SQL INSERT Statement
SQL DELETE Statement
SQL Data Types