简体   繁体   English

SQL Server更新,检查重复项

[英]SQL Server Update with check of duplicates

In SQL Server 2008 I have a table tblProfile with employee numbers ( EmID ) and profileID ( ProID ). 在SQL Server 2008中,我有一个表tblProfile其中包含员工编号( EmID )和profileID( ProID )。 EmID+ProID are a combined primary key. EmID+ProID是组合的主键。

I found "duplicates" on the EmID , which come from the employee table. 我在EmID上找到了“重复项”,这些重复项来自employee表。 Each employee can have multiple ProID , but the combination of both ID's is unique. 每个员工可以有多个ProID ,但是两个ID的组合是唯一的。

Of course, from the side of the EmID they are no duplicates, but in the employee table they are duplicates. 当然,从EmID的侧面看,它们不是重复项,但是在employee表中,它们是重复项。

For example in tblEmployee there are: 例如在tblEmployee有:

EmId        Lastname      Firstname   Birthdate  
------------------------------------------------
22          Mayer          Frank      1960-01-01  
23          Meyer          Frank      1960-01-01

Now I want to " clean " the table with this logic. 现在,我想用这种逻辑“ clean ”表。 To clean the tblEmployee is not the problem but how to clean the tblProfile with this logic? 清理tblEmployee不是问题,但是如何使用此逻辑清理tblProfile

Let's assume I want to delete EmID = 23 , because I want to keep the older record. 假设我要删除EmID = 23 ,因为我想保留旧记录。 If there is an existing profile for EmID = 23 which has not been defined for 22 it is not a problem, but if there already is a combination of 22 + 2, I would create an error when changing 23 + 2 to 22 + 2. 如果有一个尚未为22定义的EmID = 23的现有配置文件,这不是问题,但是如果已经有22 + 2的组合,则在将23 + 2更改为22 + 2时会产生错误。

Data: 数据:

EmID    ProID  
22       2  
23       2  
23       1   

In that case I would have to delete 23 + 2 but change 23 + 1 to 22 + 1. 在那种情况下,我将不得不删除23 + 2,但是将23 + 1更改为22 + 1。

The result should be 结果应该是

22       1
22       2

which is my goal. 这是我的目标。

I really do not know how to handle that. 我真的不知道该如何处理。

I cannot start with 我不能以

delete from tblprofile 
where EmID = @duplicateEmID    

as I delete the record which I might use to keep the profile.... 当我删除可用于保存个人资料的记录时...

Thanks your help 谢谢你的帮助

Michael 迈克尔

You will do this in multiple, simple steps. 您将通过多个简单步骤完成此操作。

1) Run a query to identify the Employee dupes that you want to delete (not the oldest). 1)运行查询以标识您要删除的Employee dupes(不是最旧的)。 Store them in a temp table. 将它们存储在临时表中。 Also add a column called "NewID" in the temp table. 还要在临时表中添加一个名为“ NewID”的列。

2) Run a query to update the temp table and set the value of NewID to the oldest emId for that employee. 2)运行查询以更新临时表,并将NewID的值设置为该员工的最旧的emId。

3) Run a query to insert the combination of NewID plus ProfileID into the Profile table WHERE the ProfileID currently exists with the old employee id, AND the combination of NewID & ProfileID does not already exist. 3)运行查询以将NewID和ProfileID的组合插入到Profile表中,其中ProfileID当前与旧雇员ID一起存在,并且NewID和ProfileID的组合尚不存在。

4) Delete all the rows from Profile table that have the old profileIDs in them. 4)从个人资料表中删除所有包含旧个人资料ID的行。

Breaking down a complex issue into it's most basic steps often makes it easy to solve. 将复杂的问题分解为最基本的步骤通常可以轻松解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM