如果表列具有空值,则无法检索

时间:2013-07-24 05:17:30

标签: sql sql-server-2008

我有存储程序找客户,工作正常。如果表中Customer_City_Name为空,则我无法检索该行。 SP失败了。这该怎么做 即使Customer_City_NameCustomer_Country_Code IS NULL

,我也必须得到结果
EXEC findCustomer null,'%',null,null,

SP代码:

CREATE PROCEDURE  findCustomer
  @customerNumber NVARCHAR(100),
  @customerNamePattern NVARCHAR(35),
  @customerCityNamePattern NVARCHAR(35),
  @customerCountryCode NVARCHAR(5)
AS

BEGIN
DECLARE @SQL NVARCHAR(4000)
SET @SQL =
'
SELECT
c.Customer_Number,
c.Customer_Name,
c.Postal_Address_Identifier,
c.Customer_Street_Or_Road_Name,
c.Customer_City_Name,
c.Customer_Territory_Code,
c.Customer_Postal_Code,
c.Customer_Country_Code,
c.Telephone_Number,
c.Mobile_Telephone_Number,
c.Fax_Number,
c.Email_Address 
FROM Customer c  
WHERE c.Customer_Number LIKE ' +
        CASE WHEN @customerNumber IS NOT NULL
             THEN '''' + @customerNumber + ''''
             ELSE 'c.Customer_Number'
        END + '
AND c.Customer_Name LIKE ' +
        CASE WHEN @customerNamePattern IS NOT NULL
            THEN '''' + @customerNamePattern + ''''
            ELSE 'c.Customer_Name'
        END + '
AND c.Customer_City_Name LIKE ' +
        CASE WHEN @customerCityNamePattern IS NOT NULL
            THEN '''' +@customerCityNamePattern + ''''
            ELSE 'c.Customer_City_Name'
        END + '
AND c.Customer_Country_Code LIKE ' +
        CASE WHEN @customerCountryCode IS NOT NULL
            THEN '''' +@customerCountryCode + ''''
            ELSE 'c.Customer_Country_Code'
        END

EXEC sp_executesql @SQL

5 个答案:

答案 0 :(得分:0)

@ user2218371您应该重新考虑替代方案而不是动态SQL。无论如何,这是一个替代代码。

CREATE PROCEDURE  findCustomer
  @customerNumber NVARCHAR(100),
  @customerNamePattern NVARCHAR(35),
  @customerCityNamePattern NVARCHAR(35),
  @customerCountryCode NVARCHAR(5)
AS

BEGIN
DECLARE @SQL NVARCHAR(4000)
SET @SQL =
'
SELECT
c.Customer_Number,
c.Customer_Name,
c.Postal_Address_Identifier,
c.Customer_Street_Or_Road_Name,
c.Customer_City_Name,
c.Customer_Territory_Code,
c.Customer_Postal_Code,
c.Customer_Country_Code,
c.Telephone_Number,
c.Mobile_Telephone_Number,
c.Fax_Number,
c.Email_Address 
FROM Customer c  
WHERE ' + 
    CASE WHEN  @customerNumber IS NOT NULL 
        THEN ' c.Customer_Number LIKE ''' + @customerNumber + '''' + 
            CASE WHEN  @customerNamePattern IS NOT NULL THEN ' AND ' ELSE '' END
        ELSE '' 
    END  +
    CASE WHEN   @customerNamePattern IS NOT NULL 
        THEN ' c.Customer_Name LIKE ''' +  @customerNamePattern + '''' + 
            CASE WHEN  @customerCityNamePattern IS NOT NULL THEN ' AND ' ELSE '' END
        ELSE '' 
    END  + 
    CASE WHEN   @customerCityNamePattern IS NOT NULL 
        THEN ' c.Customer_City_Name LIKE ''' +  @customerCityNamePattern + '''' + 
            CASE WHEN  +@customerCountryCode IS NOT NULL THEN ' AND ' ELSE '' END
        ELSE '' 
    END  + 
    CASE WHEN    @customerCountryCode IS NOT NULL 
        THEN ' c.Customer_Country_Code LIKE ''' +  @customerCountryCode + ''''
        ELSE '' 
    END     

EXEC sp_executesql @SQL

答案 1 :(得分:0)

当其中一个字段为NULL时,您的WHERE子句如下所示:

WHERE c.Customer_Number LIKE c.Customer_Number

这显然是故意的,因为它是CASE陈述的一部分。

但是,对于一对LIKE值,NULL不会返回true。 (NULL不是LIKE NULL。)这是NULL的典型行为。 =做同样的事情。

请参阅这个非常简单的SQL小提琴进行演示:http://sqlfiddle.com/#!3/82a23/3/0

假设我正确读取此内容,您希望在过程参数为NULL时忽略列的值。在这种情况下,您需要调整查询以完全省略该子句,或者在参数为NULL时将一些始终为真的表达式放在其中。

如果您坚持使用动态SQL构建函数(您不应该这样做),请尝试使用CASE语句:

WHERE ' +
CASE WHEN @customerNumber IS NOT NULL
     THEN 'c.Customer_Number LIKE ''' + @customerNumber + ''''
     ELSE '0=0'
END + '

答案 2 :(得分:0)

Null值无法与任何其他值进行比较,这意味着您无法获得TrueFalse。 要解决此问题,我使用ISNULL内置函数检查值是否为null,如果它返回默认值。

您应该将此函数应用于可以选择null值的每一列。

http://msdn.microsoft.com/en-us/library/ms184325.aspx

答案 3 :(得分:0)

兄弟,所以你的查询主要是执行select函数来搜索现有的客户信息

尝试修改您的where条件,如下面的示例

Select ....
From .....
WHERE (@Customer_Number is null or c.Customer_Number like @Customer_Number + '%')
AND (@customerNamePattern is null or c.Customer_Name  like @customerNamePattern + '%' )
AND (@customerCityNamePatternis null or c.Customer_City_Name   like @customerCityNamePattern+ '%' )
AND (@customerCountryCode is null or c.c.Customer_Country_Code like @customerCountryCode + '%' )

答案 4 :(得分:0)

我已经通过这样做解决了这个问题

AND ISnull(c.Customer_City_Name,'''')LIKE'+                                                     @customerCityNamePattern IS NOT NULL时的情况                                                          那么''''+ @ customerCityNamePattern +''''                                                          ELSE'isnull(c.Customer_City_Name,'''')'                                                      结束+'                                                 和Isnull(c.Customer_Country_Code,'''')喜欢'+                                                     @customerCountryCode的情况不是NULL                                                          然后''''+ @customerCountryCode +''''                                                          ELSE'isnull(c.Customer_Country_Code,'''')'                                                      结束

1.感谢asafrob我接受了你的方法。 2.感谢devio In 'LIKE' operator needs to select 'NULL'(DB) values我从中获得了想法 3.感谢jpmc26 - >我喜欢你的回答。 最后,感谢每一位回答我的问题