Table of Contents
MS SQL Server: UPDATE Command in SQL Server is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.
In SQL Server (Transact-SQL) the UPDATE command helps update the existing records on a table in a SQL Server database. There are 3 syntaxes for the UPDATE command, depending on whether you are updating the traditional style or updating a table with data from another table.
UPDATE command syntax
Syntax updating tables in SQL Server
UPDATE bangSET cot1 = bieuthuc1,cot2 = bieuthuc2,…[WHERE dieu_kien];
The syntax is to update a table with data from another table in SQL Server, combining the SELECT statement.
UPDATE boardg1SET cot1 = (SELECT bieuthuc1FROM bang2WHERE dieu_kien)[WHERE dieu_kien];
Or another syntax to update a table with data from another table
UPDATE threeng1SET bang1.cot = bang2.bieuthuc1FROM bang1INNER JOIN bang2ON (bang1.cot1 = bang2.cot1)[WHERE dieu_kien];
Variable name or variable value
cot1, cot2
Column to update.
bieuthuc1, bieuthuc2
New value should be specified for cot1, cot2. Cot1 will assign the value of bieuthuc1, cot2 assigns the value of bieuthuc2.
WHERE dieu_kien
Option. Conditions must be met in order for the record to be updated.
For instance, - update 1 column
UPDATEnhanvienSET ho = 'Johnson'WHERE nhanv= 10;
This UPDATE command will update the table employee's last name to Johnson if it is 10.
For instance, - update multiple columns
This is an example of updating more than one column with only one UPDATE command.
UPDATEnhanvienSET ten = 'Kyle',nhanvien_id = 14WHERE ho = 'Johnson';
If you want to update multiple columns, simply separate columns / values with commas.
The UPDATE command above will update the name Kyle and nhanvien_id to 14 if the person's last name is Johnson.
For instance, - update the table with data from another table
This is an example of updating the table with data from another table in MySQL.
UPDATE staffSET ten = (SELECT tenFROM danhbaWHERE danhba.ho = nhanvien.ho)WHERE nhanvien_id> 95;
The above example will update all the records in the table if nhanvien_id is greater than 95. If the employee's last name is in the table and the name is the same, the name in the list will be copied to the name in the table.
FAQ
What should you know about uPDATE command syntax?
Syntax updating tables in SQL Server.
What is MS SQL Server: UPDATE Command in SQL Server?
In SQL Server (Transact-SQL) the UPDATE command helps update the existing records on a table in a SQL Server database.
Why is MS SQL Server: UPDATE Command in SQL Server important?
A clear understanding of MS SQL Server: UPDATE Command in SQL Server helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.
Reader Comments 0
Sign in with email or Google to join the discussion.