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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help on writing a Query to delete records

Status
Not open for further replies.
Jan 30, 2002
44
CA
Hi All,

I have a 3 related tables,

ContactTable:
(PK)COntactID, (FK)CompanyRepID, ContactName, CompanyRepID, Address etc...

CompanyRepTable:
(PK)CompanyRepID, (FK)CompanyID, Location, etc..

CompanyTable:
(PK)CompanyID, account, companyname, etc


I need to do two things
1. remove from all related rows in CompanyReptable that is associated with a contact.

2. remove from CompanyTable that does not have a associated companyrep

Thanks
 
I think the following (maybe someone else can back me up ...)

1. To delete entries from CompanyRepTable that have an associated contact :
DELETE FROM CompanyRepTable
WHERE CompanyRepID IN
(SELECT DISTINCT CompanyRepID FROM ContactTable)

2. To delete entries from CompanyTable where there is no associated CompanyRepTable entry :
DELETE FROM CompanyTable
WHERE CompanyID NOT IN
(SELECT DISTINCT CompanyID FROM CompanyRepTable)

Best take a backup of the database first to be on the safe side ... ;-)

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top