简体   繁体   English

ISNULL,SQL,使用select语句作为第二个参数

[英]ISNULL, SQL, Using a select statement as the second parameter

I don't know if this is possible in SQL or if I have to write a stored procedure but I'm trying to use the ISNULL function as below so that when the parameter @sku is null I'm using a select statement to bring back all the sku's in the table: 我不知道这在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

Would be easier with an OR 使用OR会更容易

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

This was your intention, no? 这是你的意图,不是吗?

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

Notes: 笔记:

  • leading wildcards can not be optimised and won't use indexes. 前导通配符无法优化,也不会使用索引。
  • I assume you don't have a CSV in @SupplierCode for this products.supplierCode in (@SupplierCode) 我假设您在此products.supplierCode in (@SupplierCode) @SupplierCode中没有CSV products.supplierCode in (@SupplierCode)

Don't overcomplicate it. 不要过于复杂。

Make your WHERE clause: 输入您的WHERE子句:

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

You don't really explain your logic so I'm guessing, but the idea is to put a separate check for the NULL comparison. 我猜您实际上并没有解释您的逻辑,但其想法是对NULL比较进行单独检查。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM