0

我需要以名字开头的查询这是我的查询:

if(@firstName <> '')

BEGIN 
 set @queryString = @queryString+ ' and UPPER(col_FirstName) like ''%' +@firstName +'%'''
END
if(@lastName <> '')
BEGIN 

set @queryString = @queryString + ' and UPPER(col_LastName) like ''%' +@lastName +'%'''

但它在中间搜索名字我也只想用首字母搜索谢谢

4

2 回答 2

0

插入这些IFs,您可以在一个查询中执行此操作,如下所示:

...
WHERE ...
  AND (@lastName  <> '' OR UPPER(col_LastName LIKE '%@lastName%')
  AND (@firstName <> '' OR UPPER(col_FirstName LIKE '%@firstName%')
于 2012-10-27T07:54:53.610 回答
0

您构造的条件中的两个掩码都指定在列值的任何位置执行搜索:

... + ' and UPPER(col_FirstName) like ''%' + @firstName + '%'''
...
... + ' and UPPER(col_LastName) like ''%' + @lastName + '%'''

掩码中的百分号代表“任意数量的任意字符”。因此,放置在搜索词之前,它将匹配搜索词 ( @firstName) 前面有任意数量的字符的值。要指定该col_FirstName值应以该值开头@firstName(之后可能有任意数量的字符),只需删除前导百分比,即如下所示:

... + ' and UPPER(col_FirstName) like ''' + @firstName + '%'''
于 2012-10-27T08:45:49.240 回答