In SQL Server, how to use the associated multi-table UPDATE

​How to UPDATE from SELECT in SQL Server

This article describes the Inner Join update data, MERGE while updating and insertion of use.

 

Text phrase:

  1. alter the contents of a table indirectly: indirectly updates the table

  2. direct references: direct reference

  3. by using a subset of data: data subset by using

  4. secondary query statement: Auxiliary query

  5. Performing an UPDATE: execute a upadte

  6. briefly explore: a brief introduction

  7. performing this action: Do this

  8. compared to one another: compared with each other

  9. effectively synchronizes: efficient synchronization

  10. operation functions: operating functions

  11. how powerful this capability can truly be: The most powerful feature of this place

  12. The first few lines: the first few lines

  13. self-explanatory: self-explanatory

  14. in the branching logic that follows: In the subsequent branching logic

  15. matching comparative record: Compare match record

  16. have a solid understanding: in-depth 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). 

In most cases, SQL is used to update a particular table (UPDATE books SET books.title = 'The Hobbit' WHERE books.id = 1) is directly referenced performed.

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.

Sometimes, however, by using a subset of the data obtained from the secondary query to indirectly change the contents of the table may prove to be beneficial.

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. 

使用辅助Statements执行UPDATE , can be accomplished in one of two ways, depending on the SQL Server version used.

We’ll briefly explore both options so you can find what works best for you.

We will briefly introduce these two options, so that you find a way that suits you best.

Using INNER JOINS internal connections

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.

For all SQL Server installation, do this most basic method is to use INNER JOIN, so that the two different values of the column in the table compared with each other .

-- books(primary_author,author_id,title)
-- authors(name,id)

- In the table books, authors table are connected via books.author_id = authors.id. Find Title = 'The Hobbit' data, modifying 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.

In the above example, we UPDATINGbooks.primary_author= authors.nameby JOININGmatching the query to their respective two tables, matching the value of "The Hobbit" authors.idand books.author_id.

Using MERGE to UPDATE and INSERT Simultaneously using MERGE also update and insert

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. 

For SQL Server 2008 and later versions, Microsoft introduced a very useful MERGEoperation that the above INNER JOINmethod is similar, but MERGEattempt to do  UPDATEand INSERTcommand.

This effectively synchronizes the two tables based on the query performed, updating and inserting records as necessary for the two to match.

This will be performed based on the query efficiently synchronizing two tables, records and update and insert according to the two tables 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.

When using a full inquiry MERGEis certainly more complex than the basic queries INNER JOIN, but once mastered operating functions , you will quickly understand the real power of this feature .

The first few lines are rather self-explanatory:

The first few lines are self-evident :

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.

We want to use the auxiliary table authorstable MERGE INTO( UPDATEINSERT) , booksand based on the same we books.author_id = authors.idused to compare the two matches.

Where the MERGE command differs is in the branching logic that follows.

Wherein the MERGEdifferent command is in the subsequent branching logic .

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.

Here, we ask only when SQL record MATCHED- only to find when performing operations existing record. In this case, we will UPDATEperform standard as before, the books.primary_authorfield is set equal to the authors.namefield.

Finally, if the query discovers a matching comparative record that doesn’t exist, we instead perform an INSERT.

Finally, if the inquiry found that there is no matching record comparison , we will perform 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.

Here, we will simply requires SQL INSERTinsert new records into booksthe table, and transmits acquired from the association table record author_idand  primary_authorvalue of the field 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

MERGEThe final results of the statement that, for the table of each author authors, we have to verify that there is a corresponding book booksin.

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.

If a record is found, we make sure books.primary_authorto use set UPDATE, and in the absence of a match is found, we have to add a new record 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.

In this way, you should use UPDATEthe secondary compare SELECTtwo different methods in SQL statements recorded in -depth understanding .

original:

https://chartio.com/resources/tutorials/how-to-update-from-select-in-sql-server/

This article translated by the code with the world, inaccuracies please correct me, if infringement please contact us.

 

 

 

Code and the world | a number of interesting public press, two-dimensional code recognition, plus interest

Guess you like

Origin www.cnblogs.com/amusement1992/p/12018226.html