合并查询的更新语句不起作用

时间:2019-06-13 13:22:12

标签: sql-server merge-statement

我有两个表Application和Application_temp。我编写了一个合并查询,以将数据从application_temp合并到应用程序。 但是Update语句不起作用。查询将再次插入整个记录集而不是更新记录,我怀疑这一定是因为日期列,如果有人可以帮助我,那将是很好的。

我尝试了来自Google的多个来源,没有任何帮助。尝试更改各种比赛条件,但无效。

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中的任何专家都可以。帮助我吗?我过去一个月一直在挣扎。

0 个答案:

没有答案