SQL Script to Remove Data from Two Joined Tables

jspc
jspc used Ask the Experts™
on
Hello

I have two Tables in my SQL Database

Products
PSuppliers

I want to remove data from both tables.

They are linked as follows:
Products.UniqueID = PSuppliers.ProductID

Are you able to help me write a script to remove data from the Products table where the Products.Code = ‘AAA’ and in the related data PSuppliers Table where they are linked by PSuppliers.ProductID?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Lead Software Engineer
Commented:
You could do this with two separate delete statements:
delete from PSuppliers where exists (select 1 from Products where Code = 'AAA' and UniqueID = PSuppliers.ProductID)

Delete from Products where Code = 'AAA'

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial