T-SQL Tutorial

SQL UPDATE


To update column values in an existing table in SQL Server, you can use the UPDATE statement.

UPDATE Syntax:

The basic syntax is as follows:

UPDATE table_name
SET column1 = new_value1, column2 = new_value2,...
WHERE {condition}

IF you don't put the {condition} then all records on the updated column will be changed.

For example, to update the name of a customer in the "Customers" table with the ID of 1, you would use the following query:

UPDATE Customers
SET Name = 'John Smith'
WHERE ID = 1;

You can also update multiple rows at once by omitting the WHERE clause:

UPDATE Customers
SET Name = 'John Smith';

This will update all the rows in the table.

It's important to note that the UPDATE statement will update all the rows that match the WHERE clause. Be sure to use a WHERE clause that limits the update to the intended rows, or you may accidentally update more data than you intended. Also, before updating any data, it's always a good practice to take a backup of the table or data you're going to update.


Store table:

OBJECT_IDPRICENAME
1200A
2500B
3900C
4500D

Example 1:

UPDATE store
SET price = 300
WHERE object_id=1 AND name='A';

SELECT * FROM store
WHERE object_id=1 AND name='A';

OBJECT_IDPRICENAME
1300A

Example 2:

UPDATE store
SET price = 1000, name = 'Y'
WHERE object_id=3;

SELECT * FROM store
WHERE object_id=3;

OBJECT_IDPRICENAME
31000Y