如何在SQL Server 2008中返回Null in Case语句

时间:2017-03-07 06:57:50

标签: sql-server sql-server-2008

我希望在以下情况下返回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 = case(从BR_ProductPrice中选择计数(BR_ProductPrice.ID),其中BR_ProductPrice.CountryID = @CountryID)> 0然后@CountryID else Null   端

它返回以下内容: enter image description here

我希望CountryId比较else部分的case语句的null部分

  

然后@CountryID,否则Country.CountryId为空

当我通过CountryID = 1101时,查询工作正常。

3 个答案:

答案 0 :(得分:1)

在条件和条件的位置使用ISNULL(CountryID,'')并使用=''替换= null

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

答案 1 :(得分: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
    )

答案 2 :(得分: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)

从未尝试过。

希望它有效。