SQL Update Query in SQL Server Example

Learn how to update a data in SQL Table, how to write sql update query syntax in MS SQL server, in sql programming, you often need to update table record with different conditions, so in example we see how to update single row and also updating multiple row with sql where clause.

Update table_name
set column_name='new value'
where primaryKey_Id=0112

Update record in SQL database table

SQL UPDATE query is used to update / modify the existing records in any SQL table. You can use the WHERE clause (if you want to update any specific record) with the UPDATE , if there is no Where Clause specified then all the rows would be affected.

UPDATE sqlTablename
SET column1 = value1,
    column2 = value2
WHERE [condition];

Let’s assume we have some customer data in below customer table, and we want to update one particular record

tbCustomer
--+----+----------+-----+-----------+----------+-------
| Id | Name     | Code |   City         |   Mobile    |
+----+----------+-----+-----------+----------+---------
|  1 | Ajit     |  0112 | Mumbai        |  9819014758 |
|  2 | Ropesh   |  0247 | Delhi         |  7852147458 |
|  3 | Aniket   |  0142 | Kolkata       |  8852414758 |
|  4 | Brinda   |  0235 | Pune          |  9952504758 |
|  5 | Aman     |  0478 | Raipur        |  9652114758 |
|  6 | Kajal    |  0268 | Goa           |  9452565758 |
|  7 | Purnendu |  1403 | Bangalore     |  9652727538 |
+----+----------+-----+-----------+----------+--------

Now we write a query to update mobile number of a particular customer.
let's look at the query.

SQL Syntax
Update tbCustomer
set Mobile='8054069773'
where Code=0112

After executing the above query, if you again check the data in table, you will see one record has been updated

tbCustomer
--+----+----------+-----+-----------+----------+-----
| Id | Name     | Code |   City   |       Mobile   |
+----+----------+-----+-----------+----------+------
|  1 | Ajit     |  0112 | Mumbai        |  8054069773 |
|  2 | Ropesh   |  0247 | Delhi         |  7852014758 |
|  3 | Aniket   |  0142 | Kolkata       |  8852104758 |
|  4 | Brinda   |  0235 | Pune          |  9954254758 |
|  5 | Aman     |  0478 | Raipur        |  9652014758 |
|  6 | Kajal    |  0268 | Goa           |  9452605758 |
|  7 | Purnendu |  1403 | Bangalore     |  9652720758 |
+----+----------+-----+-----------+----------+--------

Note: if you don't specify where clause, all the data in that table will be updated, so be careful before executing any update statement in SQL

Whether SQL query will update single record or multiple records that will depend on where clause criteria, whatever records satisfy the criteria, will be updated with that sql statement.

You may be interested to read following posts:

MS SQL Examples | Join MS SQL Course