在SQL Server中,如何使用多表关联的UPDATE

​How to UPDATE from SELECT in SQL Server

本文介绍了Inner Join更新数据、MERGE同时更新和插入的使用。

 

文中短句:

  1. alter the contents of a table indirectly:间接地更新表的内容

  2. direct references:直接引用

  3. by using a subset of data:通过使用数据的子集

  4. secondary query statement:辅助的查询语句

  5. Performing an UPDATE:执行一条upadte

  6. briefly explore:简要介绍

  7. performing this action:执行此操作

  8. compared to one another:相互比较

  9. effectively synchronizes:有效地同步

  10. operation functions:操作功能

  11. how powerful this capability can truly be:此功能的最强大之处

  12. The first few lines :前几行

  13. self-explanatory:不言自明

  14. in the branching logic that follows:在随后的分支逻辑

  15. matching comparative record:匹配比较记录

  16. 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.

在上述例子中,我们UPDATINGbooks.primary_author=authors.name通过JOINING匹配“霍比特” 在查询到它们各自的两个表,匹配的值authors.idbooks.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尝试同时执行 UPDATEINSERT命令。

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 INTOUPDATEINSERTbooks,并且我们基于相同的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_idand 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/

本文由代码与世界翻译,不准确之处欢迎指正,如有侵权请与我们联系。

代码与世界一个有趣的公众号长按,识别二维码,加关注

猜你喜欢

转载自www.cnblogs.com/amusement1992/p/12018226.html