• RVO (3/13/2013)


    Thanks all for your response.

    Gail's blog recommends using dynamic SQL

    but our boss doesn't like it at all.

    I have to learn how to handle it either with CASE

    or IF. My problem with IF was when there are

    a lot of conditions IF becomes messy so I try to find out

    if I can write logic in WHERE clause more accurate, easy to understand.

    I'm talking about something like this:

    Pseudo code

    IF @account = '' OR LEN(@account) = 0 OR @account IS NULL

    WHERE Id1 BETWEEN 'AA' AND 'KK' AND Id2 IS NULL

    ELSEIF @account <> '' AND LEN(@account) > 0 AND @account IS NOT NULL

    WHERE Id1 BETWEEN 'MM' AND 'ZZ' AND Id2 IS NOT NULL

    ELSEIF @account > 100

    WHERE ....... and so on

    IF statements control the flow of processing. You can't use them like that in a where clause.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/