Use the merge keyword to insert or update data.

Source: Internet
Author: User

Insert or modify data using merge

CREATE procedure InsertUnitMeasure @ a nvarchar (100), @ B nvarchar (100)
As
Begin
Set nocount on
Merge temp as target -- the data target source temp is a table
Using (select @ a, @ B) as source (a, B) -- Data source
On (target. a = source. a) -- Compare Fields
When matched then update set B = source. B -- update field B if the match exists.
When not matched then insert (a, B) values (source. a, B); -- insert if it does not match
-- Output deleted. *, $ action, inserted. * into MyTempTable; -- $ action in this sentence cannot be understood and can only be considered as the type of execution.
End

A merge statement can have up to two when matched clauses. If two clauses are specified, the first clause must contain an AND
<Search_condition> clause. For any given row, only WHEN the first when matched is not applied
The second when matched clause is applied. If there are two when matched clauses, one of them must specify the UPDATE
And the other must specify the DELETE operation. If UPDATE is specified in the <merge_matched> clause
<Merge_search_condition>, if multiple rows in <table_source> match a row in target_table, SQL Server Returns an error. The MERGE statement cannot update or delete the same row multiple times.

Specify that if the row returned by <table_source> ON <merge_search_condition> does not match the row in target_table but meets other search conditions (if any), insert a row in target_table. The value to be inserted is specified by the <merge_not_matched> clause. The MERGE statement can have only one when not matched clause.

The MERGE statement can have up to two WHEN NOT MATCHED BY SOURCE
Clause. If two clauses are specified, the first clause must contain one AND <clause_search_condition>
Clause. For any given row, the second clause is applied only WHEN the first when not matched by source clause is NOT applied. If there are two WHEN
Not matched by source clause, one of them must specify the UPDATE operation, and the other must specify the DELETE operation. In
<Clause_search_condition> can only reference columns in the target table.


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.