An UPDATE
query in SQL Server (or MS SQL) is used to modify existing records in a table. Here's the basic syntax for an UPDATE
statement:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
Explanation:
table_name
: The name of the table where you want to update records.column1 = value1, column2 = value2, ...
: The columns and their new values. Separate multiple column updates with commas.WHERE condition
: The condition to specify which records should be updated. Be careful with this clause; without it, all rows in the table will be updated.
Example:
Suppose you have a table called Employees
with columns EmployeeID
, FirstName
, LastName
, and Salary
. If you want to update the salary of the employee with EmployeeID
3, you would use the following query:
UPDATE Employees SET Salary = 60000 WHERE EmployeeID = 3;
Important Notes:
- Always use a
WHERE
clause to avoid updating every row in the table. If you omit theWHERE
clause, theUPDATE
statement will apply to all rows in the table. - Back up your data before performing bulk updates, especially if you're working with important or production data.
- Test your queries on a small subset of your data or in a development environment before applying them in production.