Delete from one table with key linked to another

alex_hatzisavas

Registered User.
Local time
Today, 07:19
Joined
May 18, 2006
Messages
12
Hi all.

I kind of got stuck with this one.

I'm trying to use a Delete query to delete some records from Table A.

Let's say Table A has three fields: A1, A2 and A3

Then there's another table, Table B, with two fields: B1 and B2

Within one Delete Query, i need to delete all the records from Table A for which: A1=B1

Thanks for your help.

Alex
 
join the two tables in your query via the link you want. ie A1, B1

pull the star from table A into the query design

change the query type to a delete type

1) change to spreadsheet view, that will show you what it is going to delete first
2) when you lcick exclamation mark to run it, it will prompt you to confirm delete.

Be careful to check it, as you can delete data incorrectly very very easily.
 
Hmm, afraid not...

You see, trying to use an Inner Join kind of setup will NOT work in this situation, which was exactly my problem.

The solution (thanks Niko) is to use the below nested SQL statement:

DELETE * FROM Table1
WHERE A1 In (SELECT B1 FROM Table2)
 
And I suspect that the reason gemma's suggestion wouldn't work is that one of the tables didn't have a primary key and therefore the query when joined wasn't updateable.
 
Well actually the error message i got was:
'Could not delete from specified tables'.
 
would mean the same - to know if it wasn't an updateable query, which means it can't delete either, you could test it by changing it from a delete query to a select query and then when you open it to view, down on the status bar it would say "This recordset is not updateable" and then you would know that is why it couldn't be run as a delete query either. If it isn't updateable as a select query, it isn't updateable as anything else either.
 
The join will work once both tables have primary keys defined. All tables should have primary keys anyway.
 

Users who are viewing this thread

Back
Top Bottom