View Full Version : Delete query


jgbell
05-07-2002, 09:27 AM
Hello,
Have seen this question posted but not seen the answer I can make work yet. Have mercy.
Assumption: table1.field2 = table2.field2
Goal: delete from table1 entries found in table2
DELETE Table1.*
FROM Table1 INNER JOIN Table2 ON Table1.Field2 = Table2.Field2;

I see "Could not delete from specified tables" when I try this or various other messages when I try variants of this. What am I missing?? HELP talks about error 3086 and permissions. I bet that is a symptom and not the problem. "Select" sure works just fine... Thanks in advance.

mstorer
05-07-2002, 11:07 AM
By using DELETE Table1.* I am assuming you want to delete the entire record. I don't think you can perform a DELETE query using an join for the criteria. Try this SQL:

DELETE Table1.*, Table1.Field2
FROM Table1
WHERE (((Table1.Field2) In (Select [Field2] FROM [Table2])));

jgbell
05-07-2002, 11:17 AM
Holy Crikee! It worked verbatim. I'll believe anything you tell me from now on. Now I just need to figure out WHY that works!

Thanks for giving me a wonderful puzzle to work on for a while! Thanks so much for giving me the solution to it first!!

mstorer
05-07-2002, 02:36 PM
An action queries such as Delete requires a "predicate" (WHERE clause). If none is provided, the contents of the entire table will be deleted. An inner join, which is what you tried here, simply excludes rows for which three is no match betwen the tables being joined. Logically, it makes sense to perform the operation this way. However, by the syntax of the SQL, you would end up deleting the aspect of the [Table1] that is essential in the join. That is why the sub query works in this case.