2017-03-07 98 views
1

我想在返回null值的情況下。這是我的查詢如何在SQL Server 2008中的case語句中返回Null

Declare @MasterProduct nvarchar(50) = N'Sharepoint Easy Nav', 
     @ProductSlug nvarchar(50) = 'sharepoint-easynav-free', 
     @CountryID bigint = 1111; 

SELECT   
Product.ProductName, 
Product.MasterProductName, 
Price.ProductPrice, 
Price.ProductTax, 
Price.ProductTotalPrice, 
Product.TotalSiteCollectionText, 
Product.TotalSiteCollection, 
Product.Is_Support, 
Price.Is_Support_Price, 
Product.ProductSlug, 
Country.CountrySymbol, 
Country.CountryId 
FROM 
BR_Product as Product 
Inner Join BR_ProductPrice as Price on Product.ID = Price.ProductID 
left Join BR_Country as Country On Price.CountryID = Country.CountryId 
where Product.MasterProductName = IsNull(@MasterProduct, Product.MasterProductName) 
and Product.ProductSlug = IsNull(@ProductSlug, Product.ProductSlug) 
and Country.CountryId = case 
          when (select count(BR_ProductPrice.ID) 
           from BR_ProductPrice 
           where BR_ProductPrice.CountryID = @CountryID) > 0 
          Then @CountryID 
          else Null 
         end 

它返回我沒有行。
當我刪除

Country.CountryId =情況下,當(SELECT COUNT(BR_ProductPrice.ID)從BR_ProductPrice其中BR_ProductPrice.CountryID = @CountryID)> 0然後@CountryID別的空 端

它返回我以下: enter image description here

我想CountryId中的其他部分是這樣的

比較case語句的空部分

然後@CountryID其他Country.CountryId爲空

查詢是否工作正常,當我通過CountryID = 1101

+0

'SELECT COUNT(BR_ProductPrice.ID) CountryID'返回1? – Pream

+0

@Pream它返回我0 –

+0

檢查我的答案是否在工作 – Pream

回答

1

使用ISNULL(CountryID, '')上,其中條件和條件,並用替換= = NULL '從BR_ProductPrice 其中BR_ProductPrice.CountryID = @'

Inner Join BR_ProductPrice as Price on Product.ID = Price.ProductID 
left Join BR_Country as Country On isnull(Price.CountryID,'') = isnull(Country.CountryId,'') 
where Product.MasterProductName = ISNULL(@MasterProduct, Product.MasterProductName) 
and Product.ProductSlug = ISNULL(@ProductSlug, Product.ProductSlug) 
and ISNULL(Country.CountryId,'') = case 
         when (select count(BR_ProductPrice.ID) 
          from BR_ProductPrice 
          where ISNULL(BR_ProductPrice.CountryID,'') = @CountryID) > 0 
         Then @CountryID 
         else '' 
        end 
+0

工作與魅力。 :) –

0

你不能null使用=。試試這個:

and (
    (Country.CountryId = @CountryID 
    and (select count(BR_ProductPrice.ID) 
      from BR_ProductPrice 
      where BR_ProductPrice.CountryID = @CountryID) > 0 
    ) 
    OR Country.CountryId IS NULL 
    ) 
+0

是的但我傳遞正確的ID 1101,它返回null和1101 countryID。我只想要1個值 –

0

試圖改變這種

來自:

Country.CountryId = case 
          when (select count(BR_ProductPrice.ID) 
           from BR_ProductPrice 
           where BR_ProductPrice.CountryID = @CountryID) > 0 
          Then @CountryID 
          else Null 
         end 

要:

Country.CountryId =(select Case when cnt_BR_ProductPriceID > 0 then @CountryID else null end from 
(select count(BR_ProductPrice.ID) as cnt_BR_ProductPriceID from BR_ProductPrice where BR_ProductPrice.CountryID = @CountryID) as cnt) 

還從未嘗試過。

希望它的工作。

+0

我試過相同的模擬情況下,當沒有任何問題,這只是取決於您的查詢上面,而不是當.. –

相關問題