SQL Server MERGE中的多个更新语句

时间:2014-09-15 12:32:10

标签: sql sql-server merge sql-server-2012 sql-update

SQL Server版本:Microsoft SQL Server 2012 - 11.0.2218.0(x64)

运行此查询时出现以下异常。 例外:类型'匹配时的行为'在“更新”中不能出现多次MERGE声明的条款。

我知道异常是在Merge Statement中不止一次的Update语句。 你能否建议我如何实现以下SQL查询逻辑?

基于一栏,
               如果匹配且列不为空,则仅更新一个不同的列                当Matched和column为null时,则更新大部分列                不匹配时插入。

完整SQL

MERGE TargetTable AS targetT 
USING   SourceTable AS sourceT ON sourceT.Npi = targetT.Npi
WHEN    MATCHED AND IsNull(targetT.SPI, '') <> '' THEN
        UPDATE SET targetT.Taxonomy = sourceT.Taxonomy --Update Only One Column

WHEN    MATCHED AND IsNull(targetT.SPI,'')= '' THEN --Update Rest of the Columns
        UPDATE SET targetT.state_license_no = sourceT.state_license_no, targetT.NPI = sourceT.NPI, targetT.PrefixName = sourceT.PrefixName,targetT.last_name = sourceT.last_name,targetT.first_name = sourceT.first_name
               ,MiddleName = sourceT.MiddleName,targetT.SuffixName = sourceT.SuffixName, targetT.address_1 = sourceT.address_1,targetT.address_2 = sourceT.address_2,targetT.City = sourceT.City,targetT.State = sourceT.State
               ,zip = sourceT.zip,targetT.phone = sourceT.phone,targetT.Fax = sourceT.Fax,targetT.last_modified_date = sourceT.last_modified_date,targetT.Taxonomy = sourceT.Taxonomy           

WHEN    NOT MATCHED BY TARGET --Insert New Row
        THEN
        INSERT (state_license_no, NPI, prefixname, last_name, first_name, MiddleName, SuffixName, address_1, address_2, City, State, zip, phone, Fax, last_modified_date, Taxonomy, Data_source)
                                    VALUES (sourceT.state_license_no, sourceT.NPI, sourceT.PrefixName, sourceT.last_name, sourceT.first_name, sourceT.MiddleName, sourceT.SuffixName, 
                                    sourceT.address_1, sourceT.address_2, sourceT.City, sourceT.State, sourceT.zip, 
                                    sourceT.phone, sourceT.Fax, sourceT.last_modified_date, sourceT.Taxonomy, sourceT.Data_source);

3 个答案:

答案 0 :(得分:5)

根据MSDN,&#34;如果有两个WHEN MATCHED条款,则必须指定一个UPDATE操作,并且必须指定一个DELETE操作&#34 ;

WHEN MATCHED THEN <merge_matched>

指定target_table的所有行与ON <merge_search_condition>返回的行匹配,并满足任何其他搜索条件,根据该子句更新或删除。 MERGE语句最多可包含两个WHEN MATCHED子句。

如果指定了两个子句,则第一个子句必须附带AND <search_condition>子句。对于任何给定的行,仅当第一个不是时才应用第二个WHEN MATCHED子句。如果有两个WHEN MATCHED子句,则必须指定UPDATE操作,并且必须指定DELETE操作。

  

如果在子句中指定UPDATE,则超过   一行匹配target_table中的一行基于   ,SQL Server返回错误。

The MERGE statement cannot update the same row more than once, or update and delete the same row.

来源:MSDN

希望这有帮助。

答案 1 :(得分:2)

不要将此视为答案,我也没有测试代码。我试过的是为每一列添加一个CASE语句。

MERGE TargetTable AS targetT 
USING   SourceTable AS sourceT ON sourceT.Npi = targetT.Npi
WHEN    MATCHED THEN
        UPDATE  SET targetT.Taxonomy = CASE WHEN  IsNull(targetT.SPI, '') <> '' THEN sourceT.Taxonomy ELSE targetT.Taxonomy END,
                targetT.state_license_no = CASE WHEN IsNull(targetT.SPI,'')= '' THEN sourceT.state_license_no ELSE targetT.state_license_no END
WHEN    NOT MATCHED BY TARGET --Insert New Row
        THEN
        INSERT (state_license_no, NPI, prefixname, last_name, first_name, MiddleName, SuffixName, address_1, address_2, City, State, zip, phone, Fax, last_modified_date, Taxonomy, Data_source)
                                    VALUES (sourceT.state_license_no, sourceT.NPI, sourceT.PrefixName, sourceT.last_name, sourceT.first_name, sourceT.MiddleName, sourceT.SuffixName, 
                                    sourceT.address_1, sourceT.address_2, sourceT.City, sourceT.State, sourceT.zip, 
                                    sourceT.phone, sourceT.Fax, sourceT.last_modified_date, sourceT.Taxonomy, sourceT.Data_source);

我只为两列添加了更新。这将是漫长的,但理想情况下必须有效。

答案 2 :(得分:0)

这是一个巨大的黑客,但你可以定义一个&#34;而不是删除&#34;触发并将其用于备用更新案例。

如果在target_table上定义了任何INSTEAD OF UPDATE或INSTEAD OF DELETE触发器,则不执行更新或删除操作。而是触发触发器,并相应地填充插入和删除的表

即你可以做到&#34;匹配时......&#34; (更新)其他(删除)。而不是删除触发器可以进行更新。

这可能适用于您尝试进行&#34;软删除&#34;