Friday, February 17, 2012

Delete record selection question

I'm having trouble with the below sql command. What I'm trying todo is to delete records from tblPhotoHeader table where there are nocorresponding child records in tblPhoto.
The select statement works correctly, I'm just not sure about how toapply the syntax to correctly select the records in the deletestatement.
Any help from the experts here would be helpful.
Thanks
Tom
---------------------
DELETE FROM tblPhotoHeader
WHERE Exists
(SELECT tblPhotoHeader.photoid, photoOrderID,tblPhoto.photoType
FROM tblPhotoHeader LEFT OUTER JOIN
tblPhoto ON tblPhotoHeader.photoID = tblPhoto.photoID
WHERE (tblPhotoHeader.photoOrderID = 143)AND (tblPhoto.photoType IS NULL))
EXISTS will return TRUE of FALSE. So if your SELECT statement returnsdate the EXISTS will return TRUE. Then your SQL Statement wouldevaluate as:
DELETE FROM tblPhotoHeader WHERE TRUE which doesnt make sense. So you need to do something like :
DELETE FROM tblPhotoHeader
WHERE tblPhotoHeader.photoid IN ( (SELECT
tblPhotoHeader.photoid
FROM
tblPhotoHeader
LEFT OUTER JOIN tblPhoto
ONtblPhotoHeader.photoID = tblPhoto.photoID
WHERE (tblPhotoHeader.photoOrderID = 143)AND (tblPhoto.photoType IS NULL))

No comments:

Post a Comment