XelaIrodavlas
Registered User.
- Local time
- Today, 08:41
- Joined
- Oct 26, 2012
- Messages
- 174
I have a table (tblSchedules) which contains any number of duplicate values, and I'm trying to write some SQL to delete all but the newest record (the one with the largest auto-ID). I thought I had it cracked, but I'm getting the error "Could not delete from specified tables". Can anyone see what I'm doing wrong? Here's what I did:
first, I identified the oldest records:
Query "KeepDuplicatesMaxID"
second, using this as a subquery I compare it to the original table and identify the remaining older duplicates (which I want to delete). See below, unfortunately while I can preview the records, once I click run it doesn't work and returns the rather unhelpful "Could not delete from specified tables" error.
Query "dltOldestDuplicates"
Any ideas?
I have set Unique records on both queries to yes and there are no enforced relationships anywhere (it's a new environment I made just to test this).
TIA
first, I identified the oldest records:
Query "KeepDuplicatesMaxID"
Code:
SELECT DISTINCT tblSchedules.EmployeeName, tblSchedules.SchedDate, Max(tblSchedules.SchedID) AS MaxOfSchedID
FROM tblSchedules
GROUP BY tblSchedules.EmployeeName, tblSchedules.SchedDate
HAVING (((Count(tblSchedules.SchedID))>1));
second, using this as a subquery I compare it to the original table and identify the remaining older duplicates (which I want to delete). See below, unfortunately while I can preview the records, once I click run it doesn't work and returns the rather unhelpful "Could not delete from specified tables" error.
Query "dltOldestDuplicates"
Code:
DELETE DISTINCTROW tblSchedules.*, KeepDuplicatesMaxID.MaxOfSchedID
FROM tblSchedules LEFT JOIN KeepDuplicatesMaxID ON tblSchedules.SchedID = KeepDuplicatesMaxID.MaxOfSchedID
WHERE (((KeepDuplicatesMaxID.MaxOfSchedID) Is Null));
Any ideas?
I have set Unique records on both queries to yes and there are no enforced relationships anywhere (it's a new environment I made just to test this).
TIA