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:
-
alter the contents of a table indirectly: indirectly updates the table
-
direct references: direct reference
-
by using a subset of data: data subset by using
-
secondary query statement: Auxiliary query
-
Performing an UPDATE: execute a upadte
-
briefly explore: a brief introduction
-
performing this action: Do this
-
compared to one another: compared with each other
-
effectively synchronizes: efficient synchronization
-
operation functions: operating functions
-
how powerful this capability can truly be: The most powerful feature of this place
-
The first few lines: the first few lines
-
self-explanatory: self-explanatory
-
in the branching logic that follows: In the subsequent branching logic
-
matching comparative record: Compare match record
-
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 UPDATING
books.primary_author
= authors.name
by JOINING
matching the query to their respective two tables, matching the value of "The Hobbit" authors.id
and 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 MERGE
operation that the above INNER JOIN
method is similar, but MERGE
attempt to do UPDATE
and INSERT
command.
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 MERGE
is 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 authors
table MERGE INTO
( UPDATE
/ INSERT
) , books
and based on the same we books.author_id = authors.id
used to compare the two matches.
Where the MERGE
command differs is in the branching logic that follows.
Wherein the MERGE
different 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 UPDATE
perform standard as before, the books.primary_author
field is set equal to the authors.name
field.
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 INSERT
insert new records into books
the table, and transmits acquired from the association table record author_id
and primary_author
value 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
.
MERGE
The final results of the statement that, for the table of each author authors
, we have to verify that there is a corresponding book books
in.
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_author
to 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 UPDATE
the secondary compare SELECT
two 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