Here is one alternative:
With validEmail (EmailAddress)
As (
Select te.EmailAddress
From #tblEmails te
Except
Select tne.EmailAddress
From #tblNoEmail tne
)
Select tc.ContactID
, tc.Contact
, te.EmailID
, te.ContactID
, te.EmailAddress
From #tblContacts tc
Inner Join #tblEmails te On te.EmailID = tc.ContactID
Inner Join validEmail ve On ve.EmailAddress = te.EmailAddress;
Test for performance, of course...
Another option would be to create the no email the table exactly the same as the email table. Then, you can perform an easy insert from the email table into the no email table using all columns.
With validEmail
As (
Select te.EmailID
, te.ContactID
, te.EmailAddress
From #tblEmails te
Except
Select tne.EmailID
, tne.ContactID
, tne.EmailAddress
From #tblNoEmail tne
)
Select tc.ContactID
, tc.Contact
, ve.EmailID
, ve.ContactID
, ve.EmailAddress
From #tblContacts tc
Inner Join validEmail ve On ve.ContactID = tc.ContactID;
To further simplify the changes you need to make - create a view to replace the email table using the second version above and replace #tblEmail with your view.
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs