T-SQL Tutorial T-SQL Advanced Database Management T-SQL References

T-SQL - UPDATE Statement



The T-SQL (Transact-SQL) UPDATE statement is used to modify the existing records in a table. A WHERE clause can be used with the UPDATE statement to update the selected rows, otherwise all the rows will be assigned the updated value.

Syntax

The syntax for using UPDATE statement in T-SQL (Transact-SQL) is given below:

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

Example:

Consider a database containing a table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

  • To update the City and Salary of an employee whose EmpID is 5, the statement will be:

    UPDATE Employee
    SET City = 'Mumbai', Salary = 2900
    WHERE EmpID = 5;
    
    --see the update
    SELECT * from Employee;
    

    Now the Employee table will contain following records:

    EmpIDNameCityAgeSalary
    1JohnLondon253000
    2MarryNew York242750
    3JoParis272800
    4KimAmsterdam303100
    5RameshMumbai282900
    6HuangBeijing282800
  • Similarly, to update the Salary of John who lives in London, the following statement can be used:

    UPDATE Employee
    SET Salary = 3200
    WHERE Name = 'John' AND City = 'London';
    
    --see the update
    SELECT * from Employee;
    

    Now the Employee table will contain following records:

    EmpIDNameCityAgeSalary
    1JohnLondon253200
    2MarryNew York242750
    3JoParis272800
    4KimAmsterdam303100
    5RameshMumbai283000
    6HuangBeijing282800