Check to see if multiple VALUES exsit in a table

  • 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

    8diane1@adventure-works.com

    7dylan0@adventure-works.com

    5gail0@adventure-works.com

    9gigi0@adventure-works.com

    6jossef0@adventure-works.com

    1ken0@adventure-works.com

    3roberto0@adventure-works.com

    2terri0@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.

    [font="Verdana"]
    Today is the tomorrow you worried about yesterday:-)
    [/font]

  • 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

  • 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]

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


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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