user5021612
user5021612

Reputation:

How to use CASE as a switch

I have a function with 4 parameters, which returns an int. I want to return a results based od function parameter @Type.

I've tried:

SELECT @Result =

        CASE @Type

        WHEN 'Country' THEN
            BEGIN
                SELECT
                    IIF(COUNT(spc.DateID) = 0, 0, 1)
                FROM
                    dim.SalesPlanCountry spc
                    JOIN dim.Calendar cal ON spc.DateID = cal.DateID
                WHERE
                    cal.CalendarYear    = @Year
                    AND cal.MonthOfYear = @Month
                    AND spc.CountryID   = @CountryID
            END

        WHEN 'Region' THEN
           BEGIN        
              ...

END

Upvotes: 0

Views: 38

Answers (1)

Tanner
Tanner

Reputation: 22753

Just use IF blocks instead:

IF @Type = 'Country'
BEGIN
    SELECT * FROM Table1
END

IF @Type = 'Bob'
BEGIN
    SELECT * FROM Table2
END

Upvotes: 1

Related Questions