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.