Jared
Jared

Reputation: 2483

Expressions in Case Statements - Conditional WHERE

I am looking for a work-around or a solution for a situation where I only want to execute an expression based on two conditions using a CASE statement. I have written some mock pseudo code to illustrate what I am trying to do.

WHERE (RN = 1)
AND
    CASE
        WHEN (@ApplicationName  '' OR @ApplicationGroup  '')
            THEN 

                AND (
                    @AppFilterType = 'Groups' AND [GroupName].[Name] IN (SELECT StringValue FROM [StarTeam].[dbo].fn_CSVToTable(@ApplicationGroup))
                    OR
                    @AppFilterType = 'Apps' AND [AppName].[Name] IN (SELECT StringValue FROM [StarTeam].[dbo].fn_CSVToTable(@ApplicationName))
                )
    END

This is the idea, only execute the condition if @ApplicationName and/or @ApplicationGroup have a value.

I am using this in a T-SQL Stored Procedure running on SQL Server 2005.

Is this possible in some way?

Thanks in advance for any help.

Upvotes: 1

Views: 122

Answers (1)

D'Arcy Rittich
D'Arcy Rittich

Reputation: 171421

WHERE RN = 1
    AND (
            (@ApplicationName = '' and @ApplicationGroup = '')
            or (
                    (@AppFilterType = 'Groups' AND [GroupName].[Name] IN (SELECT StringValue FROM [StarTeam].[dbo].fn_CSVToTable(@ApplicationGroup)))
                    or (@AppFilterType = 'Apps' AND [AppName].[Name] IN (SELECT StringValue FROM [StarTeam].[dbo].fn_CSVToTable(@ApplicationName)))
                )
        )

Upvotes: 2

Related Questions