Execute the UPDATE and INSERT operations on the target table using the derived source table and MERGE.

Source: Internet
Author: User
The following example uses MERGE to update or insert rows to modify the SalesReason table. When the NewName value in the source table matches the value in the Name column of the target table (SalesReason), The ReasonType column in the target table is updated. When the value of NewName does not match, the source row is inserted into the target table. This source table is a school

The following example uses MERGE to update or insert rows to modify the SalesReason table. When the NewName value in the source table matches the value in the Name column of the target table (SalesReason), The ReasonType column in the target table is updated. When the value of NewName does not match, the source row is inserted into the target table. This source table is a school

The following example uses MERGE to update or insert rows to modify the SalesReason table. When the NewName value in the source table matches the value in the Name column of the target table (SalesReason), The ReasonType column in the target table is updated. When the value of NewName does not match, the source row is inserted into the target table. This source table is a derived table. It uses the Transact-SQL Table value constructor to specify multiple rows in the source table. For more information about using Table value constructor in a derived table, see Table value Constructor (Transact-SQL ). This example also shows how to store OUTPUT clause results in Table variables, it also describes how to summarize the results of the MERGE Statement by executing a simple selection operation to return the counts of inserted and updated rows. <无>
USE AdventureWorks2012;GO-- Create a temporary table variable to hold the output actions.DECLARE @SummaryOfChanges TABLE(Change VARCHAR(20));MERGE INTO Sales.SalesReason AS TargetUSING (VALUES ('Recommendation','Other'), ('Review', 'Marketing'), ('Internet', 'Promotion'))       AS Source (NewName, NewReasonType)ON Target.Name = Source.NewNameWHEN MATCHED THENUPDATE SET ReasonType = Source.NewReasonTypeWHEN NOT MATCHED BY TARGET THENINSERT (Name, ReasonType) VALUES (NewName, NewReasonType)OUTPUT $action INTO @SummaryOfChanges;-- Query the results of the table variable.SELECT Change, COUNT(1) AS CountPerChangeFROM @SummaryOfChangesGROUP BY Change;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.