SQL Server 如何避免合并时插入源表中的重复记录?

6jjcrrmo  于 2023-01-08  发布在  其他
关注(0)|答案(3)|浏览(140)

我进入了一个场景,我需要下载和更新一个目录,其中有代码和它的描述。所以我试图导入这些数据到一个临时表,并使用合并语句做插入/更新到我的目标表,但我结束了一个问题,我的临时表有重复的记录和合并语句,我是用来插入重复的记录到目标表。
示例:临时表包含数据

Code   Description
    C1       Desc1
    C2       Desc2
    C1       Desc1

我希望目标表数据为

Code   Description
    C1       Desc1
    C2       Desc2

我使用下面的合并语句:

MERGE INTO Frames as Target using Temp as Source
 on Source.Code=Target.Code
 WHEN MATCHED THEN  UPDATE  set Target.Description=Source.Description 
 WHEN NOT MATCHED THEN insert (Code,Description) values (Code,Description);

但最终我的目标表有了数据

Code   Description
    C1       Desc1
    C2       Desc2
    C1       Desc1

我知道这对你们中的一些人来说可能是一个简单的查询。但是我不擅长sql,所以期待一些解决我的问题的方法。
先谢了。

ehxuflar

ehxuflar1#

只是不要直接使用Temp作为源代码,而是先过滤它以获得不同的值

MERGE INTO Frames as Target 
using (SELECT DISTINCT * FROM Temp) as Source
on Source.Code=Target.Code
WHEN MATCHED THEN  UPDATE  set Target.Description=Source.Description 
WHEN NOT MATCHED THEN insert (Code,Description) values (Code,Description);
vlf7wbxs

vlf7wbxs2#

在目标表上创建唯一索引以防止重复。

chy5wohz

chy5wohz3#

另一种避免在合并时从源表插入重复记录的方法是,如果您使用的数据库不支持MERGE语句,则只需使用UPDATE和INSERT语句:

CREATE PROC [DW].[Load_Quest_Staff_Curricula_D] AS

---------- When Not Matched By Source ----------

INSERT INTO DW.Quest_Staff_Curricula_D 
(
    User_Id,
    Full_Name,
    Job_Type_Name,
    First_Name,
    Middle_Name,
    Last_Name,
    Office_Name,
    Office_Num,
    Start_Date,
    Date_Terminated,
    Curriculum_Name,
    Curriculum_Status,
    IsCurrent,
    DateCreated,
    DateModified
)

SELECT
s.Person_AD_Name AS User_Id,
s.Person_Full_Name AS Full_Name,
s.Person_Job_Type_Name AS Job_Type_Name,
s.Person_First_Name AS First_Name,
s.Person_Middle_Name AS Middle_Name, 
s.Person_Last_Name AS Last_Name,
s.Person_Office_Name AS Office_Name,
s.Person_Office_Num AS Office_Num,
s.Person_Start_Date AS Start_Date,
s.Person_Date_Terminated AS Date_Terminated,
s.Curriculum_Name AS Curriculum_Name,
s.Student_Curriculum_Status AS Curriculum_Status,
1 AS IsCurrent,
GETDATE() AS DateCreated,
GETDATE() AS DateModified
FROM (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY Person_AD_Name ORDER BY Person_AD_Name) AS Row
    FROM SABA.SABA_Curricula 
) as s 
LEFT JOIN DW.Quest_Staff_Curricula_D T
ON T.User_Id = s.Person_AD_Name
WHERE T.User_Id is NULL
AND row = 1

---------- When Matched -----------

UPDATE T
SET T.User_Id = s.Person_AD_Name,
T.Full_Name = s.Person_Full_Name,
T.Job_Type_Name = s.Person_Job_Type_Name,
T.First_Name = s.Person_First_Name,
T.Middle_Name = s.Person_Middle_Name,
T.Last_Name = s.Person_Last_Name,
T.Office_Name = s.Person_Office_Name,
T.Office_Num = s.Person_Office_Num,
T.Start_Date = s.Person_Start_Date,
T.Date_Terminated = s.Person_Date_Terminated,
T.Curriculum_Name = s.Curriculum_Name,
T.Curriculum_Status = s.Student_Curriculum_Status,
T.DateModified = GETDATE()
FROM (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY Person_AD_Name ORDER BY Person_AD_Name) AS Row
    FROM SABA.SABA_Curricula 
) as s 
LEFT JOIN DW.Quest_Staff_Curricula_D T
ON T.User_Id = s.Person_AD_Name
WHERE T.User_ID IS NOT NULL
AND row = 1

---------- When Matched -----------

UPDATE DW.Quest_Staff_Curricula_D
SET IsCurrent = 0
FROM DW.Quest_Staff_Curricula_D T
LEFT JOIN (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY Person_AD_Name ORDER BY Person_AD_Name) AS Row
    FROM SABA.SABA_Curricula 
) AS s 
ON T.User_Id = s.Person_AD_Name
WHERE row = 1
AND T.User_ID IS NULL 
AND isCurrent = 1

希望这个有用。

相关问题