MERGE INTO <target_table> USING <source>ON<join_expr> { matchedClause | notMatchedClause } [ ... ]
Transact-SQL syntax:
-- SQL Server and Azure SQL Database[ WITH <common_table_expression> [,...n] ] MERGE [ TOP ( expression ) [ PERCENT ] ] [ INTO ] <target_table> [ WITH ( <merge_hint> ) ] [ [ AS ] table_alias ] USING <table_source> [ [ AS ] table_alias ]ON<merge_search_condition> [ WHEN MATCHED [ AND <clause_search_condition> ] THEN <merge_matched> ] [ ...n ] [ WHEN NOT MATCHED [ BY TARGET ] [ AND <clause_search_condition> ] THEN <merge_not_matched> ] [ WHEN NOT MATCHED BY SOURCE [ AND <clause_search_condition> ] THEN <merge_matched> ] [ ...n ] [ <output_clause> ] [ OPTION ( <query_hint> [ ,...n ] ) ];
Example
Given the following source code:
IN -> SqlServer_01.sql
MERGEINTO targetTable WITH(KEEPIDENTITY, KEEPDEFAULTS, HOLDLOCK, IGNORE_CONSTRAINTS, IGNORE_TRIGGERS, NOLOCK, INDEX(value1, value2, value3)) as tableAliasUSING tableSource AS tableAlias2ON mergeSetCondition > mergeSetConditionWHEN MATCHED BY TARGET AND pi.Quantity - src.OrderQty >=0 THEN UPDATE SET pi.Quantity = pi.Quantity - src.OrderQtyOUTPUT $action, DELETED.v AS DELETED, INSERTED.v INSERTED INTO @localVar(col, list)OPTION(RECOMPILE);
You can expect to get something like this:
OUT -> SqlServer_01.sql
MERGE INTO targetTable as tableAliasUSING tableSource AS tableAlias2ON mergeSetCondition > mergeSetConditionWHEN MATCHED AND pi.Quantity - src.OrderQty >=0 THEN UPDATE SET pi.Quantity = pi.Quantity - src.OrderQty !!!RESOLVE EWI!!! /*** SSC-EWI-0021 - OUTPUT CLAUSE NOT SUPPORTED IN SNOWFLAKE ***/!!! OUTPUT $action, DELETED.v AS DELETED, INSERTED.v INSERTED INTO @localVar(col, list);