Noyzware
Noyzware

Reputation: 1

How to have comparison with CASE in SQL

WHERE
    CASE 
        WHEN @TypeFatt <> -1 
            THEN docAM.typCIGA = @TypeFatt 
            ELSE docAM.typCIGA > @TypeFatt 
    END

The error is

Incorrect syntax near '='

Why?

Thanks for your help

Upvotes: 0

Views: 27

Answers (1)

jarlh
jarlh

Reputation: 44795

It's generally better to use regular AND/OR constructions instead of case expressions in the WHERE clause:

WHERE (@TypeFatt <> -1 AND docAM.typCIGA = @TypeFatt)
   OR (@TypeFatt = -1  AND docAM.typCIGA > @TypeFatt)

Upvotes: 1

Related Questions