• Raul Undreiner (2/27/2013)


    What am I missing?

    Why would this work

    SELECT *

    FROM #MainQuery

    WHERE column1 IN ('Value1','Value2')

    and this not?

    declare @Var varchar(100)

    set @TOB = '''Value1'',''Value2'''

    SELECT *

    FROM #MainQuery

    WHERE column1 IN (@Var)

    -- because this

    declare @Var varchar(100)

    set @TOB = '''Value1'',''Value2'''

    SELECT *

    FROM #MainQuery

    WHERE column1 IN (@Var)

    -- is the same as this

    SELECT *

    FROM #MainQuery

    WHERE column1 IN ('Value1,Value2')

    -- which has only one value in the list

    -- and may not have a match in column1

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden