T Sql 简明教程

T-SQL - UPDATE Statement

SQL Server UPDATE 查询用于修改表中现有的记录。

The SQL Server UPDATE Query is used to modify the existing records in a table.

可以将 WHERE 子句与 UPDATE 查询结合使用以更新所选行,否则会影响所有行。

You can use WHERE clause with UPDATE query to update selected rows otherwise all the rows would be affected.

Syntax

以下是带 WHERE 子句的 UPDATE 查询的基本语法:

Following is the basic syntax of UPDATE query with WHERE clause −

UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];

可以使用 AND 或 OR 运算符组合 N 个条件。

You can combine N number of conditions using AND or OR operators.

Example

考虑包含以下记录的 CUSTOMERS 表:

Consider the CUSTOMERS table having the following records −

ID  NAME       AGE       ADDRESS             SALARY
1   Ramesh     32        Ahmedabad           2000.00
2   Khilan     25        Delhi               1500.00
3   kaushik    23        Kota                2000.00
4   Chaitali   25        Mumbai              6500.00
5   Hardik     27        Bhopal              8500.00
6   Komal      22        MP                  4500.00
7   Muffy      24        Indore              10000.00

以下命令是一个示例,用于更新 ID 为 6 的客户地址:

Following command is an example, which would update ADDRESS for a customer whose ID is 6 −

UPDATE CUSTOMERS
SET ADDRESS = 'Pune'
WHERE ID = 6;

客户表现在将具有以下记录:

CUSTOMERS table will now have the following records −

ID  NAME       AGE       ADDRESS             SALARY
1   Ramesh     32        Ahmedabad           2000.00
2   Khilan     25        Delhi               1500.00
3   kaushik    23        Kota                2000.00
4   Chaitali   25        Mumbai              6500.00
5   Hardik     27        Bhopal              8500.00
6   Komal      22        Pune                4500.00
7   Muffy      24        Indore              10000.00

如果要修改 CUSTOMERS 表中的所有 ADDRESS 和 SALARY 列值,则不需要使用 WHERE 子句。UPDATE 查询如下:

If you want to modify all ADDRESS and SALARY column values in CUSTOMERS table, you do not need to use WHERE clause. UPDATE query would be as follows −

UPDATE CUSTOMERS
SET ADDRESS = 'Pune', SALARY = 1000.00;

CUSTOMERS 表现在将具有以下记录。

CUSTOMERS table will now have the following records.

ID  NAME       AGE       ADDRESS          SALARY
1   Ramesh     32        Pune             1000.00
2   Khilan     25        Pune             1000.00
3   kaushik    23        Pune             1000.00
4   Chaitali   25        Pune             1000.00
5   Hardik     27        Pune             1000.00
6   Komal      22        Pune             1000.00
7   Muffy      24        Pune             1000.00