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

发布于 2024-12-12 06:56:48 字数 1082 浏览 0 评论 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

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘香 2024-12-19 06:56:48

用 OR 会更容易

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

这是你的意图,不是吗?

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

注意:

  • 前导通配符无法优化,也不会使用索引。
  • 我假设您在 @SupplierCode 中没有此 products.supplierCode in (@SupplierCode) 的 CSV

Would be easier with an 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)
悍妇囚夫 2024-12-19 06:56:48

不要把它搞得太复杂。

创建您的 WHERE 子句:

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

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

Don't overcomplicate it.

Make your WHERE clause:

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.

马蹄踏│碎落叶 2024-12-19 06:56:48
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文