T-SQL Tutorial
T-SQL Useful Resources
Selected Reading
- T-SQL - Numeric Functions
- T-SQL - Date Functions
- T-SQL - String Functions
- T-SQL - Functions
- T-SQL - Indexes
- T-SQL - Transactions
- T-SQL - Stored Procedures
- T-SQL - Sub-Queries
- T-SQL - Joining Tables
- T-SQL - DISTINCT Clause
- T-SQL - GROUP BY Clause
- T-SQL - ORDER BY Clause
- T-SQL - LIKE Clause
- T-SQL - WHERE Clause
- T-SQL - DELETE Statement
- T-SQL - UPDATE Statement
- T-SQL - SELECT Statement
- T-SQL - INSERT Statement
- T-SQL - Drop Tables
- T-SQL - Create Tables
- T-SQL - Data Types
- T-SQL - Overview
- T-SQL - Home
T-SQL Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
T-SQL - UPDATE Statement
T-SQL - UPDATE Statement
The SQL Server UPDATE Query is used to modify the existing records in a table.
You can use WHERE clause with UPDATE query to update selected rows otherwise all the rows would be affected.
Syntax
Following is the basic syntax of UPDATE query with WHERE clause −
UPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN WHERE [condition];
You can combine N number of conditions using AND or OR operators.
Example
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 Chaitap 25 Mumbai 6500.00 5 Hardik 27 Bhopal 8500.00 6 Komal 22 MP 4500.00 7 Muffy 24 Indore 10000.00
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 Chaitap 25 Mumbai 6500.00 5 Hardik 27 Bhopal 8500.00 6 Komal 22 Pune 4500.00 7 Muffy 24 Indore 10000.00
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 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 Chaitap 25 Pune 1000.00 5 Hardik 27 Pune 1000.00 6 Komal 22 Pune 1000.00 7 Muffy 24 Pune 1000.00Advertisements