【发布时间】:2011-07-07 14:11:44
【问题描述】:
我们最近遇到了一些问题,即从表中删除重复条目的 sql 脚本不会使用最近的条目作为要保留的条目。我认为这条线是问题
delete from vaccine_patient_details
where vacc_pat_guid <>
(Select top 1 vacc_pat_guid
from vaccine_patient_details as v
where v.patient_guid = patient_guid and
v.vaccine_guid = vaccine_guid
order by date_given desc)
这是正确的语法吗?我发现另一个版本的脚本在不同的表上工作。 (名称已更改以匹配第一个示例)
delete from vaccine_patient_details
where vacc_pat_guid <>
(Select top 1 vacc_pat_guid
from vaccine_patient_details as v
where v.patient_guid = vaccine_patient_details.patient_guid and
v.vaccine_guid = vaccine_patient_details.vaccine_guid
order by date_given desc)
这个使用内部where子句中已删除表的表名,这会导致我的第一个版本出现问题吗?
关于表的详细信息:
- 任何以 guid 结尾的列都是 uniqueidentifier 的数据类型
- vacc_pat_guid 是主键并且是唯一的。
- date_given 是一个可以为空的日期时间。如果存在一个为空且一个不为空的重复项,则应该首选非空的一个。
【问题讨论】:
标签: sql sql-server sql-server-2005