0

我不知道这在 SQL 中是否可行,或者我是否必须编写存储过程,但我正在尝试使用 ISNULL 函数,如下所示,以便当参数 @sku 为 null 时,我使用 select 语句带来返回表中的所有 sku:

SELECT     GooglePrice.idGooglePrice, GooglePrice.idProduct,  products.sku, products.wholeprice, products.price as CurrentHMMPrice, GooglePrice.bestPrice, GooglePrice.link, GooglePrice.title, GooglePrice.description, GooglePrice.ourPrice as PriceCompHMMPrice, 
                      GooglePrice.searchType, GooglePrice.shippingCost, GooglePrice.cheapestOrder, GooglePrice.timeStamp, 
    'ShippingCostNew' = CASE
        WHEN GooglePrice.shippingCost = -1 THEN 'N/A'
        WHEN GooglePrice.shippingCost = 0 THEN 'Free Shipping'
        WHEN GooglePrice.shippingCost > 0 Then cast(GooglePrice.shippingCost as varchar)
        END
FROM         GooglePrice INNER JOIN
                      products ON GooglePrice.idProduct = products.idProduct
WHERE     (products.supplierCode in (@SupplierCode)) AND ISNULL((products.sku like '%'+@sku+'%'), (products.sku in (select sku from products where products.sku)))

ORDER BY GooglePrice.idGooglePrice
4

3 回答 3

3

使用 OR 会更容易

WHERE
     (products.supplierCode in (@SupplierCode)) 
     AND 
     (products.sku like '%'+@SupplierCode+'%' OR @SupplierCode IS NULL)

这是你的意图,不是吗?

     AND 
     products.sku like ISNULL('%'+@SupplierCode+'%',products.sku)

笔记:

  • 前导通配符无法优化,也不会使用索引。
  • 我假设您在 @SupplierCode 中没有为此的 CSV products.supplierCode in (@SupplierCode)
于 2011-10-25T18:49:17.397 回答
1

不要过于复杂。

制定你的WHERE条款:

WHERE     
   ((products.supplierCode in (@SupplierCode)
       AND 
   (products.sku like '%'+@SupplierCode+'%'))
OR (@suppliercode IS NULL)

你并没有真正解释你的逻辑,所以我猜,但想法是单独检查NULL比较。

于 2011-10-25T18:49:02.470 回答
0
SELECT GooglePrice.idGooglePrice, GooglePrice.idProduct,  products.sku, products.wholeprice, products.price as CurrentHMMPrice, GooglePrice.bestPrice, GooglePrice.link, GooglePrice.title, GooglePrice.description, GooglePrice.ourPrice as PriceCompHMMPrice, GooglePrice.searchType, GooglePrice.shippingCost, GooglePrice.cheapestOrder, GooglePrice.timeStamp, 
    'ShippingCostNew' = CASE
        WHEN GooglePrice.shippingCost = -1 THEN 'N/A'
        WHEN GooglePrice.shippingCost = 0 THEN 'Free Shipping'
        WHEN GooglePrice.shippingCost > 0 Then cast(GooglePrice.shippingCost as varchar)
        END
FROM  GooglePrice INNER JOIN
      products ON GooglePrice.idProduct = products.idProduct
WHERE (products.supplierCode in (@SupplierCode)) AND (@SupplierCode is null or products.sku like '%'+@SupplierCode+'%')
ORDER BY GooglePrice.idGooglePrice
于 2011-10-25T18:50:03.980 回答