DELETE FROM CustomerAttachmentXTab 
WHERE CustomerNumber NOT IN (SELECT CustomerNumber 
                             FROM Customer WITH (NOLOCK) 
                             WHERE MarkAsDeleted = 0)

DELETE FROM Attachment  
WHERE 
  AttachmentNumber NOT IN (SELECT AttachmentNumber
                           FROM CustomerAttachmentXTab WITH (NOLOCK)) AND
  AttachmentNumber NOT IN (SELECT AttachmentNumber
                           FROM ReservationAttachmentXTab WITH (NOLOCK)) AND
  AttachmentNumber NOT IN (SELECT FK_CertificateAttachmentID 
                           FROM ProductCertificate WITH (NOLOCK))                           
                           
EXEC DeleteInvalidCustomers

DELETE Topwarelist WHERE Pk_LinkNumber IS NULL OR Fk_Department IS NULL OR WareNumber IS NULL OR Fk_WaregroupNumber IS NULL

