【问题标题】:How to return Null in Case statement in SQL Server 2008如何在 SQL Server 2008 中的 Case 语句中返回 Null
【发布时间】:2017-07-27 07:04:27
【问题描述】:

我想返回空值以防万一。这是我的查询

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 when (select count(BR_ProductPrice.ID) from BR_ProductPrice where BR_ProductPrice.CountryID = @CountryID) > 0 Then @CountryID else Null 结束

它返回我以下内容:

我希望 CountryId 比较 else 部分中 case 语句的 null 部分,类似这样

然后@CountryID 否则 Country.CountryId 为空

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

【问题讨论】:

  • select count(BR_ProductPrice.ID) from BR_ProductPrice where BR_ProductPrice.CountryID = @CountryID 返回 1?
  • @Pream 它返回我 0
  • 检查我下面的答案是否有效

标签: sql-server sql-server-2008


【解决方案1】:

在 where 条件和条件上使用 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

【讨论】:

  • 带着魅力工作。! :)
【解决方案2】:

尝试改变这一点

来自:

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)

从未尝试过。

希望它有效。

【讨论】:

  • 我已经尝试过同样的模拟没有任何问题的情况,它只是取决于你上面的查询而不是如果..
【解决方案3】:

您不能将=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
    )

【讨论】:

  • 是的,但我传递了正确的 ID 1101,它返回 null 和 1101 countryID。我只想要 1 个值
猜你喜欢
  • 2013-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-13
相关资源
最近更新 更多