【问题标题】:ISNULL, SQL, Using a select statement as the second parameterISNULL, SQL, 使用select语句作为第二个参数
【发布时间】:2011-10-25 18:45:45
【问题描述】:

我不知道这在 SQL 中是否可行,或者我是否必须编写存储过程,但我正在尝试使用 ISNULL 函数,如下所示,以便当参数 @sku 为空时,我正在使用选择恢复表中所有 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

【问题讨论】:

  • 如果这个参数为null,我想带回所有的sku。

标签: sql isnull


【解决方案1】:

使用 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

【讨论】:

    【解决方案2】:

    不要过于复杂。

    制作你的WHERE 子句:

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

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

    【讨论】:

      【解决方案3】:
      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
      

      【讨论】:

        猜你喜欢
        • 2019-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多