SQL Delete using Join?

q1w2e3r4t7

Active member
Joined
Nov 15, 2005
Messages
30
I have 2 tables (Transactions, Entries)
They have a relationship of one Transaction.ID to many Entried.TransIDs.

i want to be able to delete delete a list of Transactions & the associated linked entries based on a Transaction.Naration Value.

How is this possible?

i have tried;
Code:
DELETE  
FROM   dbo.Entries 
INNER JOIN   dbo.Transactions ON dbo.Entries.TransID = dbo.Transactions.ID
WHERE (dbo.Transactions.Naration LIKE " & "Sales" & ")

but gives me the following error ;
Code:
Incorrect Syntax near the keyword INNER as system.data.sqlclient.sqlconnection.onerror

Im only just starting to learn SQL and unsure of whats going wrong here, any assistance would be greatly appreciated.


Thanks, (p.s. running vs.2005 VB if it helps)
 
I found an example and it has assisted me deleting records from the Entries table using data from the Transactions table,
i had to adjust

Code:
DELETE  
FROM   dbo.Entries

TO

Code:
DELETE  FROM   dbo.Entries
FROM   dbo.Entries"

HOWEVER, can anyone please help me adjust this further to also delete the Transaction rows also, or must this be done in a seperate command?

Thanks
 
They must be deleted separately - a DELETE can only work on one table. You can use alternatives, if your DB has the features. For example, in SQL Server you can turn on cascading deletes (delete from the parent table and it will delete the child rows). You can also code similar functionality in a trigger.

It is BY FAR much more common to do the two deletes yourself, deleting from the child tables first and then the parent. Ive worked with both kinds of companies - those that prefer to do deletes "by hand" and those that LOVE triggers. I only recently learned about the ability to do cascading deletes and my companys DBA doesnt like them. I trust him enough to take his advice.

-ner
 
Back
Top