Sometimes you may need to change multiple column values in SQL Server. You can modify multiple column values using a single UPDATE statement. In this article, we will look at how to update multiple columns in SQL Server.
How to Update Multiple Columns in SQL Server
Here is the syntax to update multiple columns in SQL Server using UPDATE statement.
UPDATE table_name SET column1 = value1, column2 = value2, ... [WHERE condition];
In the above SQL statement, you need to specify table name, and mention column names and their new values as a comma-separated list, and optionally mention a WHERE condition to update only certain rows in your table.
Also read : How to Insert Data in SQL Server
For example, let us say you have the following table.
# create table sales(id int, order_date date, amount int); # insert into sales(id, order_date, amount) values(1, '2020-12-01', 100), (2,'2020-12-02',250), (3,'2020-12-03',300); # select * from sales; id | order_date | amount ----+------------+-------- 1 | 2020-12-01 | 100 2 | 2020-12-02 | 250 3 | 2020-12-03 | 300
Also read : How to Create View in SQL Server
Here is the SQL query to update order_date and amount columns in sales table where id=3.
# update sales set order_date='2020-12-04', amount=250 where id=3; # select * from sales; id | order_date | amount ----+------------+-------- 1 | 2020-12-01 | 100 2 | 2020-12-02 | 250 3 | 2020-12-04 | 250
As you can see it is very easy to update multiple columns in SQL Server.