Dynamic WHERE IN statement

  • Hello,

    I am trying to create dynamic WHERE IN statement.

    declare @ReportType TINYINT

    set @ReportType=1

    SELECT SUM(EnrollCnt)

    FROM ReportA

    WHERE [Year] = 2006

    AND BranchID IN (

    CASE @ReportType

    WHEN 1 THEN ('10000023')

    WHEN 2 THEN (SELECT BranchID FROM Branch WHERE GMOfficeID = '10000006')

    END )

    When @ReportType=1 it works, but when @ReportType=2

    it gives error message: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, = or when the subquery is used as an expression.

    Sure, subquery should return more than 1 value, this is the point!

    Any thoughts? Thank you.

  • If you must do this inside the SQL statement (and would not like to use a top level if else statement, you

    could do something like:

    SELECT SUM(EnrollCnt)

    FROM ReportA

    WHERE [Year] = 2006

    AND BranchID IN ( SELECT BranchID

    FROM Branch

    WHERE GMOfficeID = '10000006'

    AND 1 = @ReportType

    UNION ALL

    SELECT '10000023' AS BranchID

    WHERE 2 = @ReportType )

    Regards,

    Andras


    Andras Belokosztolszki, MCPD, PhD
    GoldenGate Software

  • Move this into an INNER JOIN

    SELECT SUM(EnrollCnt)

    FROM ReportA

    INNER JOIN Branch

    ON (@ReportType = 1 AND BranchID = '10000023')

    OR

    (@ReportType = 2 AND GMOfficeID = '10000006')

    WHERE [Year] = 2006

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg
  • Wow, what a fast response.

    Thank you, Andras, for good idea.:)

  • The problem is that subqueries can't return multiple values in some cases. Your data probably violates that. Try one of the suggestions above.

  • Here is another way to do it that keeps the case statement instead of a union. There is almost always more than one way to handle the problem:

    declare @ReportType TINYINT

    set @ReportType=1;

    with CTE1 as (

    select

    case

    when @ReportType = '1' then '10000006'

    when @ReportType = '2' then BranchID

    end as Branchid

    From

    ReportA

    where

    [Year] = 2006

    and (GMOfficeID = '10000006' or @ReportType = '1')

    )

    SELECT

    SUM(EnrollCnt)

    FROM

    ReportA

    WHERE

    [Year] = 2006

    AND BranchID IN (select * from CTE1 )

    ---
    Timothy A Wiseman
    SQL Blog: http://timothyawiseman.wordpress.com/

  • My opinion is the INNER JOIN method above is more straight forward, but then again I'm biased 😀

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg
  • Thank you one more time. I think that example with CTE1 is most flexible.

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply