3

我在 SQL 中有一个查询,目前如果用户在文本框中输入的所有值都为空,那么当他们单击搜索时,它只会返回整个表。

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”,那么我希望所有在伦敦打电话给 John 的人都可以返回。

4

6 回答 6

7
(FirstName LIKE '%' + @FirstName + '%' OR @FirstName IS NULL )
于 2013-01-10T16:58:35.357 回答
2

Other answers are correct but I thought i'd help by putting something back into your original query:

 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))

You should just be able to copy and paste this out

Jim

于 2013-01-10T17:17:47.520 回答
2

所以你只想考虑@FirstName它是否有价值?你可以通过改变来做到这一点

FirstName LIKE '%' + @FirstName + '%'

对此

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

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

(@FirstName IS NULL OR FirstName LIKE '%' + @FirstName + '%')
于 2013-01-10T16:57:51.940 回答
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, '') + '%'
于 2013-01-11T18:20:41.897 回答
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
于 2013-01-10T17:11:38.030 回答
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

于 2013-01-10T17:19:53.880 回答