How to use Update command in SQL Server?

Introduction:

In this article i will explain what is Update command and how to use Update command in SQL Server.

Description:

 In previous articles i explained How to create a table in SQL ServerHow to alter table column in SQL ServerHow to add a column to table and How to delete a column from table in SQL Server, and How to rename a column and How to rename a table in SQL Server. Now i will explain what is Update command and how to use Update command in SQL Server.

Here i'm using Employee table.

Update command is used to update particluar records of a table. While updating records we can specify conditions in where clause. Where clause is optional.

Syntax:

Update <table-name> set <column-name1> = '<new-value1>', <column-name2> = '<new-value2>'

We can specify more than one column for update.

Example:(1)

Now I'm updating all employees salaries by 1000.

update Employee set EmpSalary = EmpSalary + 1000

Output:

Example:(2)

Now we will see another example with where clause

Our requirement is update EmpName where EmpNo is 2.

update Employee set EmpName = 'Sagar Reddy' where EmpNo = 2

Output:

Note: In previous example 3 rows updated. Here only 1 row updated beacause we are updating based on EmpNo.