IF里面的where子句 - SQL

时间:2012-02-01 04:02:21

标签: sql-server-2008

我可以在where子句中使用if语句,如下所示。

SELECT DISTINCT *
FROM product p
          INNER JOIN product_to_vendor pv
        ON pv.product_id = p.product_id
WHERE pv.vendor_id = @vendorId AND p.site_id = @siteId AND
            IF (@productStatus < 4)
        BEGIN 
            p.[rank] = @productStatus 
        END

感谢

1 个答案:

答案 0 :(得分:1)

SELECT DISTINCT *
FROM product p
INNER JOIN product_to_vendor pv
        ON pv.product_id = p.product_id
WHERE     pv.vendor_id = @vendorId 
      AND p.site_id = @siteId 
      AND (@productStatus < 4 AND p.[rank] = @productStatus)

我不确定您的要求,但这里显示了如何从布尔逻辑构造if-else。只需假设if (condition)只是AND的另一个语句。