Link to home
Start Free TrialLog in
Avatar of Metalteck
MetalteckFlag for United States of America

asked on

Oracle SQL Delete with multiple tables

I am trying to write a delete statement that will remove all the joined records from the following query:
delete from prod.apcinvoice apc
WHERE EXISTS
  (SELECT *
from prod.apcinvoice apc,
     prod.apcdistrib apd,
     prod.apcattach att
where apc.company = apd.company (+)
and   apc.vendor = apd.vendor (+)
and   apc.edi_nbr = apd.edi_nbr (+)
and   apc.invoice = apd.invoice (+)
and   apc.suffix = apd.suffix (+)
and   apc.company = att.company (+)
and   apc.vendor = att.vendor (+)
and   apc.invoice = att.invoice (+)
and   apc.invoice_dte = TO_DATE('2018-01-29', 'YYYY-MM-DD'));

When I executed the nested query, I get a total of 538 records.
But when I execute everything, I get a total of 650.

What am I missing to delete the correct amount of records?
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany 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
SOLUTION
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
SOLUTION
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
Avatar of Metalteck

ASKER

@Mark, I understand your point, but that would leave all the records from the other 2 tables that I would want to delete.
That is a misunderstanding on your side, metalteck. You are just querying different tables, not delete their content. A delete  statement always acts on a single table (or view) only.
If these tables have a foreign key relationship defined with "on delete cascade" specified in the foreign keys, then Oracle will automatically delete the related child table records when the parent table records are deleted.  

If there are no foreign keys defined between these tables, then Qlemo's comment is correct: an Oracle "delete..." statement affects one table only.  You then have to do separate "delete..." statements for each table that you want to delete records from.