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 に答える