I’m very new to scripting and SQL and I’ve inherited a crazy database generated by some contact management software. I’m running Microsoft SQL Server 2005. What I’m trying to do is select rows that meet certain conditions (the most important being that BIRTHDATE is today’s date, which seems like it may be my issue). Here is my code:
SELECT
TBL_CONTACT.BIRTHDATE,
TBL_CONTACT.COMPANYNAME,
TBL_CONTACT.CATEGORY,
CUST_ContactTable1_074000.CUST_DesignatedAgent_074203199,
TBL_EMAIL.ADDRESS
FROM
TBL_CONTACT,
CUST_ContactTable1_074000,
TBL_EMAIL
WHERE
TBL_CONTACT.BIRTHDATE >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) AND
TBL_CONTACT.BIRTHDATE < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 1) AND
TBL_CONTACT.CATEGORY = 'Active' AND TBL_EMAIL.ADDRESS IS NOT NULL AND
CUST_ContactTable1_074000.CUST_DesignatedAgent_074203199 IS NOT NULL
The issue is that it’s returning all the rows in my database (minus the records not meeting the NOT NULL and CATEGORY = ‘Active’ conditions, it appears) even though there should be only one row that has the correct TBL_CONTACT.BIRTHDATE. It is putting the BIRTHDATE of the only row that should work down the entire list, even though those rows should have BIRTHDATE’s that aren’t today’s date. If I make it so there are no BIRTHDATE’s that match today’s, nothing gets returned (which is what I expect).
What am I doing wrong? Please explain your answer as I am looking to learn. Thank you!
Where are your join conditions between table?
As it stands, you have a cross join a.k.a Cartesian product