How to Update Table Records in SQL Server

You can use the following query in order to update records in SQL Server:

UPDATE [database_name].[table_schema].[table_name]
SET column1 = value1, column2 = value2, ...
WHERE condition

Next, you’ll see how to apply the above query using a simple example.

The Example

Let’s suppose that you created a table in SQL Server, where:

  • The table name (with the schema) is: dbo.Person
  • The table has 3 columns: FirstName, City and Age
  • The database name is: TestDatabase

The ‘Person’ table also contains the following 6 records:

Example of a table

Let’s now see how to update the records in the table across the 3 scenarios below.

Scenario 1: Update a Single Record in SQL Server

To start with a simple example, let’s say that you want to update the last record in the ‘Person’ table (this record represents Jon), where:

  • The City will be updated to ‘Bangkok
  • The Age will be updated to 52

You can then run the query below to perform the update:

UPDATE [TestDatabase].[dbo].[Person]
SET City = 'Bangkok', Age = 52
WHERE FirstName = 'Jon'

To verify that the record got updated to the desired values, let’s run the following SELECT query:

SELECT * FROM [TestDatabase].[dbo].[Person]

As you can see, the city is now ‘Bangkok’ and the age is 52:

How to Update Table Records in SQL Server

Scenario 2: Update Multiple Records in SQL Server

In this example, we’ll update 2 records. Specifically, we will update the city from ‘London’ to ‘Tokyo’ using this query:

UPDATE [TestDatabase].[dbo].[Person]
SET City = 'Tokyo'
WHERE City = 'London'

Now let’s rerun the SELECT query in order to observe the latest changes:

SELECT * FROM [TestDatabase].[dbo].[Person]

You’ll now see that the city was indeed updated to ‘Tokyo’ for the two records:

Update Table Records in SQL Server

Scenario 3: Update an Entire Column

Lastly, let’s update the ‘Age’ column by increasing the age by 1 across all the records:

UPDATE [TestDatabase].[dbo].[Person]
SET Age = Age + 1

Run the SELECT query to observe the results:

SELECT * FROM [TestDatabase].[dbo].[Person]

As you can see, the age increased by 1 across all the records in the ‘Person’ table:

Update an entire column

You may also want to learn how to: