我有一个MS Access数据库,并且正在尝试对名为tblPrevious
的表进行重复数据删除。我创建了一个记录集,该记录集仅从表中选择不同的值,并且我想使用该记录集覆盖原始表。我无法使用普通的make表查询,因为我不能使用tblPrevious
作为目标表和源表。因此,我的计划是通过vba创建一个记录集,然后删除原始表,然后使用该记录集填充一个与原始tblPrevious
同名的新表。我的另一个选择是创建一个名为tblPrevious2
的新表,然后删除tblPrevious
,然后将tblPrevious2
重命名为tblPrevious
。这对我来说似乎太混乱了。下面是我尝试的vba,但是我收到一条失败消息,内容为“ Runtim Error 3078,Microsoft Access数据库引擎找不到输入表或查询“ rst”。在此先感谢您的帮助!
Sub RemovePreviousDupes()
Dim rst As Recordset Set rst = CurrentDb.OpenRecordset(Name:="DeDupetblPreviousExport", Type:=RecordsetTypeEnum.dbOpenDynaset)
strSQL = "SELECT rst.* INTO DELETETABLE FROM rst;"
DoCmd.RunSQL (strSQL)
End Sub
为DeDupetblPreviousExport
添加了SQL。还要注意,在上面的代码中,我只是尝试将记录集写到新表中,如果要使其工作,我将尝试删除tblPrevious
,并且最后一行将写入{{1 }},而不是tblPrevious
DELETETABLE
答案 0 :(得分:2)
不。您不能以记录集为源(或者实际上是任何查询)来运行操作查询。
请使用记录集的源作为查询的源。
如果要将记录集写入表,还可以考虑通过VBA创建表,然后使用循环将记录写入表。
答案 1 :(得分:2)
假设您的表维护一个唯一的主键,例如自动编号, id ,请考虑使用纯SQL对原始表进行重复数据删除,并避免使用临时表或记录集。具体来说,在DELETE
子句中运行IN
,以保持与所有匹配列对应的最小 id 。
DELETE FROM tblPreviousExport main
WHERE id IN
(SELECT sub.id
FROM tblPreviousExport sub
WHERE sub.id > main.id
AND sub.[Month Reported] = main.[Month Reported]
AND sub.[Employee SSN] = main.[Employee SSN]
AND sub.[Employee First Name] = main.[Employee First Name]
AND sub.[Employee Last Name] = main.[Employee Last Name]
AND sub.Pool = main.Pool
AND sub.OpCo = main.OpCo
AND sub.Paygroup = main.Paygroup
AND sub.Pool = main.Pool
AND sub.[Effective Date] = main.[Effective Date]
AND sub.[Medical Plan] = main.[Medical Plan]
AND sub.[Coverage Level] = main.[Coverage Level]
AND sub.Type = main.Type
AND sub.[Tier Change Effective Date] = main.[Tier Change Effective Date]
AND sub.[Num Eligible Months] = main.[Num Eligible Months]
AND sub.[Employee Ongoing Contribution] = main.[Employee Ongoing Contribution]
AND sub.[Full Employer Contribution] = main.[Full Employer Contribution]
AND sub.[Prorated Employer Contribution] = main.[Prorated Employer Contribution]
AND sub.[ER Contribution Already Received] = main.[ER Contribution Already Received]
AND sub.[Total Contribution] = main.[Total Contribution]
AND sub.[Max Contribution] = main.[Max Contribution])
或带有EXISTS
子句:
DELETE FROM tblPreviousExport main
WHERE id EXISTS
(SELECT 1
FROM tblPreviousExport sub
WHERE sub.id > main.id
AND sub.[Month Reported] = main.[Month Reported]
AND sub.[Employee SSN] = main.[Employee SSN]
AND sub.[Employee First Name] = main.[Employee First Name]
AND sub.[Employee Last Name] = main.[Employee Last Name]
AND sub.Pool = main.Pool
AND sub.OpCo = main.OpCo
AND sub.Paygroup = main.Paygroup
AND sub.Pool = main.Pool
AND sub.[Effective Date] = main.[Effective Date]
AND sub.[Medical Plan] = main.[Medical Plan]
AND sub.[Coverage Level] = main.[Coverage Level]
AND sub.Type = main.Type
AND sub.[Tier Change Effective Date] = main.[Tier Change Effective Date]
AND sub.[Num Eligible Months] = main.[Num Eligible Months]
AND sub.[Employee Ongoing Contribution] = main.[Employee Ongoing Contribution]
AND sub.[Full Employer Contribution] = main.[Full Employer Contribution]
AND sub.[Prorated Employer Contribution] = main.[Prorated Employer Contribution]
AND sub.[ER Contribution Already Received] = main.[ER Contribution Already Received]
AND sub.[Total Contribution] = main.[Total Contribution]
AND sub.[Max Contribution] = main.[Max Contribution])
答案 2 :(得分:0)
您说的很接近解决方案: 我的另一个选择是创建一个名为tblPrevious2的新表,然后删除tblPrevious,然后将tblPrevious2重命名为tblPrevious
相反,容易工作的是: 创建一个名为tblPrevious2的新表,然后通过一个简单的Delete Query删除tblPrevious的记录(NOT THE TABLE ITSELF),然后将tblPrevious2记录追加到tblPrevious
答案 3 :(得分:0)
感谢大家的回应,感谢您提供帮助。我最终做的是我原本以为是一种混乱的方式,但最终要容易得多。我运行了一个生成表查询,仅选择不同的记录,并将新表命名为tblPrevious2。然后我只运行了DoCmd.Rename "tblPrevious", acTable, "tblPrevious2"
,它只是用新表替换了旧表。这比我预期的要干净得多,而且效果很好。