0

我有两个表 Application 和 Application_temp 。我编写了一个合并查询来将数据从 application_temp 合并到 application。但更新声明不起作用。查询再次插入整组记录而不是更新,我怀疑这一定是因为日期列,如果有人可以帮助我,那就太好了。

我尝试了谷歌的多个来源,没有任何帮助。尝试更改各种匹配条件但不起作用。

CREATE PROCEDURE [dbo].[MergeApplication_tempToApplication]
AS
--CREATE UNIQUE INDEX cmdbid ON Application_Temp(cmdb_id) WITH 
(IGNORE_DUP_KEY = OFF)

MERGE INTO Application AS Target
USING Application_temp AS Source
    ON (
            --Target.CMDB_ID=Source.CMDB_ID
            --AND Target.Application_Name=Source.Application_Name
            Target.maas_application_id = source.maas_application_id
            )
WHEN MATCHED
    AND (
        Target.CMDB_ID <> Source.CMDB_ID
        OR Target.Application_Name <> Source.Application_Name
        OR Target.Fss_Portfolio <> Source.Fss_Portfolio
        OR Target.Managed_By <> Source.Managed_By
        --Target.Date_Created <> Source.Date_Created OR
        --Target.Date_Updated <> Source.Date_Updated
        )
    THEN
        UPDATE
        SET Target.cmdb_id = Source.cmdb_id
            ,Target.Application_Name = Source.Application_Name
            ,Target.Fss_Portfolio = Source.Fss_Portfolio
            ,Target.Managed_By = Source.Managed_By
            ,Target.Date_Created = Source.Date_Created
            ,Target.Date_Updated = Source.Date_Updated
WHEN NOT MATCHED
    THEN
        INSERT (
            Application_Name
            ,cmdb_id
            ,Fss_Portfolio
            ,Managed_By
            ,Date_Created
            ,Date_Updated
            )
        VALUES (
            source.Application_Name
            ,source.cmdb_id
            ,source.Fss_Portfolio
            ,source.Managed_By
            ,source.Date_Created
            ,source.Date_Updated
            );

正如我所说,更新声明不起作用。任何 SQL Server 专家都可以。帮我解决这个问题?从上个月开始,我一直在挣扎。

4

0 回答 0