0

I need to be able to do an IF ELSE in the having clause of a select statement. Here is an excerpt of what I am trying to do:

HAVING  
IF      @Vendor IS NOT NULL 
THEN    (COUNT(Iron_1.SN) > 0) AND [User].UserIdentifier = @Vendor AND Location_1.LocationName = @LocationName
ELSE

(COUNT(Iron_1.SN) > 0) AND Location_1.LocationName = @LocationName

I want certain filters based on if the user provided a value for a query parameter or not.

4

1 回答 1

3

试试这个

HAVING COUNT(Iron_1.SN) > 0 AND Location_1.LocationName = @LocationName AND
        (@Vendor IS NULL OR (@Vendor IS NOT NULL AND [User].UserIdentifier = @Vendor))

或者这个稍微简单的版本

HAVING COUNT(Iron_1.SN) > 0 AND Location_1.LocationName = @LocationName AND
        (@Vendor IS NULL OR [User].UserIdentifier = @Vendor)

尝试这两种,然后选择你最喜欢的并且有效的

于 2013-06-13T16:03:30.827 回答