Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Check to see if multiple VALUES exsit in a table Expand / Collapse
Author
Message
Posted Friday, December 06, 2013 11:04 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 3:15 PM
Points: 209, Visits: 501
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:
EmailAddressID EmailAddress
8 diane1@adventure-works.com
7 dylan0@adventure-works.com
5 gail0@adventure-works.com
9 gigi0@adventure-works.com
6 jossef0@adventure-works.com
1 ken0@adventure-works.com
3 roberto0@adventure-works.com
2 terri0@adventure-works.com
*/

-- 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.



Today is the tomorrow you worried about yesterday
Post #1520685
Posted Friday, December 06, 2013 12:55 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, January 03, 2014 3:52 PM
Points: 6,066, Visits: 5,277
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
Post #1520721
Posted Friday, December 06, 2013 1:32 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 3:15 PM
Points: 209, Visits: 501
Thank you sir. I should start using temp tables. I was able to achive what I wanted.


Today is the tomorrow you worried about yesterday
Post #1520737
Posted Friday, December 06, 2013 5:42 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 4:00 PM
Points: 35,967, Visits: 30,258
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
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1520779
Posted Monday, December 09, 2013 4:36 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, April 10, 2014 3:49 PM
Points: 366, Visits: 577
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

Post #1521016
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse