Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Check to see if multiple VALUES exsit in a table


Check to see if multiple VALUES exsit in a table

Author
Message
Minnesota - Viking
Minnesota - Viking
SSC Veteran
SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)

Group: General Forum Members
Points: 217 Visits: 564
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:-)

Elliott Whitlow
Elliott Whitlow
SSCertifiable
SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)

Group: General Forum Members
Points: 6208 Visits: 5314
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
Minnesota - Viking
Minnesota - Viking
SSC Veteran
SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)

Group: General Forum Members
Points: 217 Visits: 564
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:-)

Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45018 Visits: 39887
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
aaron.reese
aaron.reese
Mr or Mrs. 500
Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)

Group: General Forum Members
Points: 589 Visits: 898
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search