26

我正在 SQL Server 中运行合并。在我的更新中,我只想在值发生变化时更新该行。每次更新都会增加一个版本行。下面是一个例子:

MERGE Employee as tgt USING 
(SELECT Employee_History.Emp_ID
, Employee_History.First_Name
, Employee_History.Last_Name
FROM Employee_History)
as src (Emp_ID,First_Name,Last_Name)
ON tgt.Emp_ID = src.Emp_ID
WHEN MATCHED THEN 
    UPDATE SET
    Emp_ID = src.Emp_ID,
    ,[VERSION] = tgt.VERSION + 1 
    ,First_Name = src.First_Name
    ,Last_Name = src.Last_Name
WHEN NOT MATCHED BY target THEN 
    INSERT (Emp_ID,0,First_Name,Last_Name)
VALUES 
    (src.Emp_ID,[VERSION],src.First_Name,src.Last_Name);

现在,如果我只想更新行,从而增加版本,只有在名称发生变化的情况下。

4

4 回答 4

50

WHEN MATCHED可以有AND。此外,无需更新EMP_ID.

...
 WHEN MATCHED AND (trg.First_Name <> src.First_Name 
   OR trg.Last_Name <> src.Last_Name) THEN UPDATE
   SET 
   [VERSION] = tgt.VERSION + 1 
    ,First_Name = src.First_Name
    ,Last_Name = src.Last_Name
 ...

如果 Last_Name 或 First_Name 可以为空,则在比较 trg.Last_Name <> src.Last_Name 时需要注意NULL值,例如ISNULL(trg.Last_Name,'') <> ISNULL(src.Last_Name,'')

于 2013-04-19T14:48:57.513 回答
2

a1ex07 提供的答案是正确的答案,但我只是想扩展比较大量列、观察空值等的难度。

我发现我可以在一些带有哈希字节的 CTE 中生成校验和,在合并中定位这些 CTE,然后使用上面指定的“更新和....”条件来比较哈希:

with SourcePermissions as (
    SELECT 1 as Code, 1013 as ObjectTypeCode, 'Create Market' as ActionName, null as ModuleCode, 1 as AssignableTargetFlags
    union all SELECT 2, 1013, 'View Market', null, 1
    union all SELECT 3, 1013, 'Edit Market', null, 1
    --...shortened....
)
,SourcePermissions2 as (
    select sp.*, HASHBYTES('sha2_256', xmlcol)  as [Checksum] 
    from SourcePermissions sp
    cross apply (select sp.* for xml raw) x(xmlcol)
)
,TargetPermissions as (
    select p.*, HASHBYTES('sha2_256', xmlcol)  as [Checksum] 
    from Permission p
    cross apply (select p.* for xml raw) x(xmlcol)
) --select * from SourcePermissions2 sp join TargetPermissions tp on sp.code=tp.code where sp.Checksum = tp.Checksum

    MERGE TargetPermissions AS target  
    USING (select * from SourcePermissions2) AS source ([Code] , [ObjectTypeCode] , [ActionName] , [ModuleCode] , [AssignableTargetFlags], [Checksum])  
        ON (target.Code = source.Code)  
    WHEN MATCHED and source.[Checksum] != target.[Checksum] then
        UPDATE SET [ObjectTypeCode] = source.[ObjectTypeCode], [ActionName]=source.[ActionName], [ModuleCode]=source.[ModuleCode], [AssignableTargetFlags] = source.[AssignableTargetFlags]
    WHEN NOT MATCHED THEN  
        INSERT ([Code] , [ObjectTypeCode] , [ActionName] , [ModuleCode] , [AssignableTargetFlags])  
        VALUES (source.[Code] , source.[ObjectTypeCode] , source.[ActionName] , source.[ModuleCode] , source.[AssignableTargetFlags])
    OUTPUT deleted.*, $action, inserted.[Code] 
        --only minor issue is that you can no longer do a inserted.* here since it gives error 404 (sql, not web), complaining about returning checksum which is included in the target cte but not the underlying table
        ,inserted.[ObjectTypeCode] , inserted.[ActionName] , inserted.[ModuleCode] , inserted.[AssignableTargetFlags]
    ;

几点注意事项:我本可以使用校验和或 binary_checksum 大大简化,但我总是与这些发生冲突。

至于“为什么”,这是自动部署的一部分,以使查找表保持最新。合并的问题在于有一个复杂且大量使用的索引视图,因此对相关表的更新非常昂贵。

于 2020-02-24T19:28:48.597 回答
1

您可以更改[VERSION] + 1代码以在名称匹配时添加零,而不是完全避免更新:

[VERSION] = tgt.VERSION + (CASE
    WHEN tgt.First_Name <> src.First_Name OR tgt.Last_Name <> src.Last_Name
    THEN 1
    ELSE 0 END)
于 2013-04-19T14:48:22.987 回答
-1

@a1ex07 感谢您的回答.. 稍微更正.. 我没有关注 SQL 版本,所以这可能是 SQL 规范的更改

当匹配和条件然后更新

以上不是有效的语法

以下有效

匹配时更新设置...条件不匹配时插入...

所以会把它改成

WHEN MATCHED THEN UPDATE
   SET 
   [VERSION] = tgt.VERSION + 1 
   ,First_Name = src.First_Name
   ,Last_Name = src.Last_Name

WHERE trg.First_Name <> src.First_Name 或 trg.Last_Name <> src.Last_Name

https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9016.htm#SQLRF01606

于 2020-04-24T02:44:24.800 回答