December 6, 2013 at 11:04 am
I know how to check for a sinle vlaue but how do I chekc to see if multiple values exist. I need to check for certain email addresses from a list that I have.
Let us say I ahve 3 email addresses, I want to check for all of them in a table and for eevery email address that is present I want to print something like "You email address is XXX" and if one of those 3 is not found my results should look like
"You email address is XXX"
YYYYY not found
"You email address is ZZZZ"
Please suggest.
I'm atatching some TSQL that I tried on [AdventureWorks2012].[Person].[EmailAddress]
/****** Select ALL if where an email address is present in the list ******/
SELECT EmailAddressID,EmailAddress
FROM [AdventureWorks2012].[Person].[EmailAddress]
WHERE EmailAddress IN
(
'ken0@adventure-works.com', --1
'terri0@adventure-works.com', --2
'roberto0@adventure-works.com',--3
'gail0@adventure-works.com', --4
'jossef0@adventure-works.com',--5
'dylan0@adventure-works.com',--6
'diane1@adventure-works.com',--7
'gigi0@adventure-works.com',--8
-- The below emails do not exist.
'25terri0@adventure-works.com',--9
'25roberto0@adventure-works.com',--10
'25rob0@adventure-works.com'--11
)
/*
Results:
EmailAddressIDEmailAddress
*/
-- Test to see if a single email address is present
IF EXISTS
(
SELECT EmailAddress FROM [AdventureWorks2012].[Person].[EmailAddress]
WHERE EmailAddress IN ('25rob0@adventure-works.com')
)
BEGIN
SELECT 'Email address is presnt'
END
ELSE
SELECT 'Email Address is NOT present';
/*
Results:
Email Address is NOT present
*/
-- Test if multiple values are present:
IF EXISTS
(
SELECT EmailAddress FROM [AdventureWorks2012].[Person].[EmailAddress]
WHERE EmailAddress IN
(
'ken0@adventure-works.com', --1
'terri0@adventure-works.com', --2
'roberto0@adventure-works.com',--3
'gail0@adventure-works.com', --4
'jossef0@adventure-works.com',--5
'dylan0@adventure-works.com',--6
'diane1@adventure-works.com',--7
'gigi0@adventure-works.com',--8
-- The below emails do not exist.
'25terri0@adventure-works.com',--9
'25roberto0@adventure-works.com',--10
'25rob0@adventure-works.com'--11
)
)
BEGIN
SELECT 'Email address is presnt'
END
ELSE
SELECT 'Email Address is NOT present';
/*
Results:
Email Address is present
*/
when I check multiples using EXISTS it works as per its design and says YES even if a single item is present. Please advise.
[font="Verdana"]
Today is the tomorrow you worried about yesterday:-)[/font]
December 6, 2013 at 12:55 pm
I don't have adventureworks installed so I'll have to wing it..
If you get the email list into a table it becomes pretty easy..
DECLARE @Wk TABLE ( RecId INT IDENTITY(1,1) NOT NULL, EmailAddress VARCHAR(100) NOT NULL )
INSERT @Wk (EmailAddress ) VALUES ( 'ken0@adventure-works.com' ),
( 'terri0@adventure-works.com' ),
( 'roberto0@adventure-works.com' ),
( 'gail0@adventure-works.com' ),
( 'jossef0@adventure-works.com' ),
( 'dylan0@adventure-works.com' ),
( 'diane1@adventure-works.com' ),
( 'gigi0@adventure-works.com' ),
( '25terri0@adventure-works.com' ),
( '25roberto0@adventure-works.com' ),
( '25rob0@adventure-works.com' )
SELECT States = CASE WHEN ea.EmailAddressID IS NULL
THEN 'Email Address Is NOT Present'
ELSE 'Email Address Is Present'
END
EmailAddressID,
w.EmailAddress
FROM @Wk w LEFT OUTER JOIN [AdventureWorks2012].[Person].[EmailAddress] ea
ON w.EmailAddress = ea.EmailAddress
GO
CEWII
December 6, 2013 at 1:32 pm
Thank you sir. I should start using temp tables. I was able to achive what I wanted.
[font="Verdana"]
Today is the tomorrow you worried about yesterday:-)[/font]
December 6, 2013 at 5:42 pm
Minnesota - Viking (12/6/2013)
Thank you sir. I should start using temp tables. I was able to achive what I wanted.
The "Temp tables" you're speaking of was actually a Table Variable and it was just being used as a source of test data. It actually had little to do with the problem.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 9, 2013 at 4:36 am
Yup, Its a standard LEFT JOIN problem.
give me all the records in the starting table (list of email addresses supplied) and the matching record in the right hand table (stored email addresses) if it exists
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply