Link to home
Start Free TrialLog in
Avatar of mtrussell
mtrussell

asked on

Specify the table containg the records you want to delete

This it appears to be simple but getting all the stars to align on the SQL query is proving to be a pain.  

Basically, I have in Table1 a list all the IDs I want to delete out of Table2

The key field name in both tables is called ID.  What I want to do is delete all the records from Table2 that are equal to the ID in Table 1.

I have done this in a delete query with a join with the below code but it is giving be the above error

DELETE Tbl_ClientFlightControl.ID
FROM Tbl_ClientFlightControl INNER JOIN tbl_tmp_NotAsia_Clients ON Tbl_ClientFlightControl.ID = tbl_tmp_NotAsia_Clients.ID;


How do I fix the SQL code to get the desired results?
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Try with:

DELETE *
FROM Tbl_ClientFlightControl 
WHERE ID IN (SELECT ID FROM tbl_tmp_NotAsia_Clients.ID);

Open in new window

/gustav
Avatar of mtrussell
mtrussell

ASKER

It is returning an error code saying 'Could not find file H:\Data\tbl_tmp_NotAsia_Clients.ID'

Never seen anything like this before...  weird....
Then, I guess, your table isn't linked properly.

/gustav
Its a normal table.  It's not linked... not sure... let me play around with this some more...  It's just two normal tables in a database.  I'm not even sure why it is looking on the H drive.
try this with minor amendment:

DELETE  FROM Tbl_ClientFlightControl WHERE ID IN (SELECT ID FROM tbl_tmp_NotAsia_Clients)
Yes, it should read tbl_tmp_NotAsia_Clients, not tbl_tmp_NotAsia_Clients.ID

/gustav
OK that works but now I have another issue... The ID is an Autonumber so when I create a temporary table it is taking the first number and creating an autonumber from there and doing a sequential numbering instead of just importing the ID number from Table 1...   (Access really shouldn't be this hard on something so simple...  :(

Anyway, I get the same error but how about if I skip the temporary table all together and just build it off a query...

What I want to do is delete all the records from Table 1 which are not part of a group.

DELETE Tbl_ClientFlightControl.*
FROM Tbl_ClientFlightControl LEFT JOIN Tbl_Regions ON Tbl_ClientFlightControl.Location = Tbl_Regions.Location
WHERE (((Tbl_Regions.Region) Not Like "*Americas*"));

Is there a simple way of doing this?
Use the same syntax: ... where id in (select Id from YourQuery)

where YourQuery selects from the join you mention.

/gustav
But what about all the not where Location = [x]

sorry for being so slow on all this but I've been hunting and pecking at a solution for a couple hours now.  I'm a bit in over my head on this and it looks like it should be pretty simple.

This what I have but the Not Like isn't right.  :(


DELETE  FROM Tbl_ClientFlightControl WHERE Location IN (SELECT Location FROM tbl_regions Not Like "*Americas*"));
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That worked....  thanks... one last one... I need to do the same but there is a table in the middle to get to the same result... could you point out how this one works as well?

DELETE Tbl_ClientCommunication.*
FROM (Tbl_ClientFlightControl RIGHT JOIN Tbl_ClientCommunication ON Tbl_ClientFlightControl.Primary_ID = Tbl_ClientCommunication.UCL) LEFT JOIN Tbl_Regions ON Tbl_ClientFlightControl.Location = Tbl_Regions.Location
WHERE (((Tbl_Regions.Region) Not Like "*America*"));

Really appreciate all your help on this...
Create the query that lists the records to be deleted - perhaps using the query wizard.

Then insert the resulting SQL to replace the IN (...) above.

/gustav
This isn't an unmatched query.  Basically, there is a field in Table 1 which links it to Table 2 which we did above.  What I am needing is to link a common identifier to Table 3.
The code I did it with is above.  I would have no idea how to reference table 3 in what you showed me or I would at least make an attempt at this.