Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find orphaned records in two related tables

Status
Not open for further replies.

lyudmila

Programmer
Oct 18, 2002
54
US
I have two related tables, Invoice Header and Invoice Detail. They have two key fields: Vendor# and Invoice#. What is the best way to find orphaned records - records in Detail table that have no match in Header table?
 
select * from InvoiceHeader a full outer join InvoiceDetail b on a.Vendor# = b.Vendor# and a.Invoice# = b.Invoice#

In the result you would see the null rows either from InvoiceDetail or from InvoiceHeader where you could identify the orphand records
 
to expand on that do

Code:
SELECT ....
  FROM InvoiceHeader IH
  FULL OUTER JOIN InvoiceDetail ID
    ON IH.Vendor# = ID.Vendor#
   AND IH.Invoice# = ID.Invoice#
 WHERE IH.Invoice# IS NULL
    OR ID.Invoice# IS NULL

The WHERE clause will filter out the records that are not orphaned leaving you with just the parent and child records that have no relationship.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top