How to UPDATE from SELECT in SQL Server
本文介绍了Inner Join更新数据、MERGE同时更新和插入的使用。
文中短句:
-
alter the contents of a table indirectly:间接地更新表的内容
-
direct references:直接引用
-
by using a subset of data:通过使用数据的子集
-
secondary query statement:辅助的查询语句
-
Performing an UPDATE:执行一条upadte
-
briefly explore:简要介绍
-
performing this action:执行此操作
-
compared to one another:相互比较
-
effectively synchronizes:有效地同步
-
operation functions:操作功能
-
how powerful this capability can truly be:此功能的最强大之处
-
The first few lines :前几行
-
self-explanatory:不言自明
-
in the branching logic that follows:在随后的分支逻辑
-
matching comparative record:匹配比较记录
-
have a solid understanding:有深入的了解
Under most circumstances, SQL updates are performed using direct references to a particular table (UPDATE books SET books.title = 'The Hobbit' WHERE books.id = 1
).
在大多数情况下,SQL更新是使用对特定表(UPDATE books SET books.title = 'The Hobbit' WHERE books.id = 1)的直接引用来执行的。
Yet, on occasion, it may prove beneficial to alter the contents of a table indirectly, by using a subset of data obtained from secondary query statement.
但是,有时候,通过使用从辅助查询语句获得的数据子集,来间接更改表的内容可能被证明是有益的。
Performing an UPDATE
using a secondary SELECT
statement can be accomplished in one of two ways, primarily depending upon which version of SQL Server you are using.
使用辅助
语句来
执行UPDATE
,可以通过以下两种方法之一来完成,这主要取决于所使用的SQL Server版本。
We’ll briefly explore both options so you can find what works best for you.
我们将简要介绍这两个选项,以便您找到最适合您的方法。
Using INNER JOINS 使用内部连接
For all SQL Server installations, the most basic method of performing this action is to use an INNER JOIN
, whereby values in the columns of two different tables are compared to one another.
对于所有SQL Server安装,执行此操作的最基本方法是使用INNER JOIN
,从而将两个不同表的列中的值相互比较。
-- books(primary_author,author_id,title)
-- authors(name,id)
-- 在books表中,通过books.author_id = authors.id 来连接authors表。查找书名='The Hobbit'的数据,修改books.primary_author = authors.name
UPDATE books
SET books.primary_author = authors.name
FROM books
INNER JOIN authors
ON books.author_id = authors.id
WHERE books.title = 'The Hobbit'
In the above example, we’re UPDATING
the books.primary_author
field to match the authors.name
for ‘The Hobbit’ by JOINING
both tables in the query to their respective, matching values of authors.id
and books.author_id
.
在上述例子中,我们UPDATING
books.primary_author
=authors.name
通过JOINING
匹配“霍比特” 在查询到它们各自的两个表,匹配的值authors.id
和books.author_id
。
Using MERGE to UPDATE and INSERT Simultaneously 使用MERGE同时更新和插入
For SQL Server 2008 and newer, Microsoft introduced the exceptionally useful MERGE
operation which is similar to the above INNER JOIN
method, but MERGE
attempts to perform both an UPDATE
and an INSERT
command together.
对于SQL Server 2008和更高版本,Microsoft引入了非常有用的MERGE
操作,该操作与上述INNER JOIN
方法类似,但是MERGE
尝试同时执行 UPDATE
和INSERT
命令。
This effectively synchronizes the two tables based on the query performed, updating and inserting records as necessary for the two to match.
这将根据执行的查询有效地同步两个表,并根据需要更新和插入记录以使两个表匹配。
-- books(primary_author,author_id,title)
-- authors(name,id)
MERGE INTO books
USING authors
ON books.author_id = authors.id
WHEN MATCHED THEN
UPDATE SET
books.primary_author = authors.name
WHEN NOT MATCHED THEN
INSERT
(books.author_id, books.primary_author)
VALUES
(authors.id, authors.name)
The full query when using MERGE
is certainly a bit more complex then that of a basic INNER JOIN
, but once you grasp how the operation functions, you’ll quickly understand how powerful this capability can truly be.
使用时的完整查询MERGE
肯定比基本查询复杂一些INNER JOIN
,但是一旦掌握了操作功能,您将很快了解此功能的真正强大之处。
The first few lines are rather self-explanatory:
前几行是不言自明的:
MERGE INTO books
USING authors
ON books.author_id = authors.id
We want to MERGE INTO
(UPDATE
/INSERT
) the books
table by using the secondary authors
table, and we’re matching the two based on the same books.author_id = authors.id
comparison.
我们想要使用辅助表authors
表来MERGE INTO
(UPDATE
/ INSERT
)books
,并且我们基于相同的books.author_id = authors.id
比较来匹配两者。
Where the MERGE
command differs is in the branching logic that follows.
其中MERGE
命令不同的是在随后的分支逻辑。
WHEN MATCHED THEN
UPDATE SET
books.primary_author = authors.name
Here we’re asking SQL to perform an action only when records MATCHED
– when an existing record is found. In that case, we perform a standard UPDATE
just as we did before, setting the books.primary_author
field to equal the authors.name
field.
在这里,我们要求SQL仅在记录时MATCHED
–当找到现有记录时才执行操作。在这种情况下,我们将UPDATE
像以前一样执行标准,将books.primary_author
字段设置等于authors.name
字段。
Finally, if the query discovers a matching comparative record that doesn’t exist, we instead perform an INSERT
.
最后,如果查询发现不存在的匹配比较记录,我们将执行INSERT
。
WHEN NOT MATCHED THEN
INSERT
(books.author_id, books.primary_author)
VALUES
(authors.id, authors.name)
Here we’re simply asking SQL to INSERT
a new record into the books
table and passing along the values for the author_id
and primary_author
fields, grabbed from the associated authors
table record.
在这里,我们只是简单地要求SQL将INSERT
新记录插入books
表中,并传递从关联表记录中获取的author_id
and primary_author
字段的值authors
。
The end result of our MERGE
statement is that for every author in the authors
table, we verify whether a corresponding book exists in books
.
MERGE
语句的最终结果是,对于表中的每个作者authors
,我们都验证是否存在相应的书在books
中。
If a record is found, we ensure books.primary_author
is set using UPDATE
, and where no match is found, we add a new record to books
.
如果找到了一条记录,我们确保books.primary_author
使用进行设置UPDATE
,并且在没有找到匹配项的情况下,我们向添加新记录books
。
With that, you should have a solid understanding of two different methods that can be used to UPDATE
records in SQL by using secondary, comparative SELECT
statements.
这样,您应该对使用UPDATE
辅助比较SELECT
语句在SQL中进行记录的两种不同方法有深入的了解。
原文:
https://chartio.com/resources/tutorials/how-to-update-from-select-in-sql-server/
本文由代码与世界翻译,不准确之处欢迎指正,如有侵权请与我们联系。
代码与世界∣一个有趣的公众号长按,识别二维码,加关注