如果它为null,则不要在SQL WHERE子句中包含变量

时间:2013-01-10 16:56:40

标签: c# asp.net sql search

我在SQL中有一个查询,当前如果用户在文本框中输入的所有值都为null,当他们点击搜索时它将返回整个表。

SELECT Column Names
FROM TableName
WHERE
        FirstName LIKE '%' + @FirstName + '%'
        OR Surname LIKE '%' + @Surname + '%'
        OR City LIKE '%' + @City + '%'
        OR County LIKE '%' + @County + '%'

但是,我想要做的是在搜索时没有输入任何内容,例如FirstName。因此,如果有人决定将“London”输入City,那么它将仅返回包含伦敦的结果。同样,如果他们输入“John”和“London”,那么我希望每个人都能在伦敦打电话给约翰。

6 个答案:

答案 0 :(得分:7)

(FirstName LIKE '%' + @FirstName + '%' OR @FirstName IS NULL )

答案 1 :(得分:2)

所以你只想考虑@FirstName是否有价值?您可以通过更改

来实现
FirstName LIKE '%' + @FirstName + '%'

到这个

(@FirstName = '' OR FirstName LIKE '%' + @FirstName + '%')

或者当然如果@FirstName以null而不是空字符串形式出现,那么它将是

(@FirstName IS NULL OR FirstName LIKE '%' + @FirstName + '%')

答案 2 :(得分:2)

其他答案是正确的但我认为我会帮助您将原因放回原始查询中:

 SELECT Column Names
FROM TableName
WHERE
  ((@FirstName is null and FirstName like '%') or (isnull(FirstName ,0) = @FirstName)) and
  ((@Surname is null and Surname like '%') or (isnull(Surname ,0) = @Surname)) and
  ((@City is null and City like '%') or (isnull(City ,0) = @City)) and
  ((@County is null and County like '%') or (isnull(County,0) = @County))

你应该能够复制并粘贴它

吉姆

答案 3 :(得分:0)

它更麻烦,但根据您的数据集和索引的大小,您可以通过这条路线获得一些性能提升。

Declare @FirstName Nvarchar(10),
        @SurName Nvarchar(10),
        @City Nvarchar(10),
        @County Nvarchar(10),
        @SQL Nvarchar(Max),
        @OrCriteria Nvarchar(Max) = ''

Set     @SQL = 'Select  Column Names
                From    TableName '

If      @FirstName Is Not Null
Begin
        Set     @OrCriteria = @OrCriteria + 'FirstName Like ''%''' + @FirstName + '''%'' Or '
End         

If      @Surname Is Not Null
Begin
        Set     @OrCriteria = @OrCriteria + 'Surname Like ''%''' + @Surname + '''%'' Or '
End     

If      @City Is Not Null
Begin
        Set     @OrCriteria = @OrCriteria + 'City Like ''%''' + @City + '''%'' Or '
End     

If      @County Is Not Null
Begin
        Set     @OrCriteria = @OrCriteria + 'County Like ''%''' + @County + '''%'' Or '
End         

If      @OrCriteria <> ''
Begin
        Set     @SQL = @SQL + 'Where (' + Left(@OrCriteria,Len(@OrCriteria)-4) + ')'
End

Exec    sp_executeSQL @SQL

答案 4 :(得分:0)

declare @sql as varchar(300)
, @FirstName as varchar(50)
, @Surname as varchar(50)
, @City as varchar(50)
, @County as varchar(50)

set  @FirstName = 'myname'
set  @Surname = 'mylastname'
set  @City = null
set  @County = 'county'

set @sql= 'SELECT Column_Names
FROM TableName ' +    
 case when rtrim(ltrim(isnull(@FirstName,'') + isnull(@Surname,'') + isnull(@City,'') + isnull(@County,''))) = '' then '' else 'where ' end
 + coalesce('FirstName LIKE ''%' + nullif(@FirstName,'')+ '%'' or ', '') +
        + coalesce('Surname LIKE ''%' + nullif(@Surname,'')+ '%'' or ', '') +
        + coalesce('City LIKE ''%' + nullif(@City,'')+ '%'' or ', '') +
        + coalesce('County LIKE ''%' + nullif(@County,'')+ '%''', '')

    select @sql

@sql的内容应为

SELECT Column_Names  FROM TableName where FirstName LIKE '%myname%' or Surname LIKE %mylastname%' or County LIKE '%county%'

然后,要使用您创建的动态字符串,您需要Exec @Sql

答案 5 :(得分:0)

在SQL Server中存在ISNULL()

然后

SELECT Column Names
FROM TableName
WHERE
        FirstName LIKE '%' + ISNULL((@FirstName, '') + '%'
        OR Surname LIKE '%' + ISNULL((@Surname, '') + '%'
        OR City LIKE '%' + ISNULL((@City, '') + '%'
        OR County LIKE '%' + ISNULL((@County, '') + '%'