开发者

Multiple Search Criteria Single Field in T-SQL

I have an is开发者_如何学运维sue where I need to pass multiple criteria for a single field in SQL. For example if I was looking for all book titles which contained Ruby, PHP, or ASP.Net. Also, I only want to return a limited number of rows for pagination. I know I could do dynamic sql, but I am sure there is probably a better way. Any help is appreciated.


If you put your searchwords in a table it is not so hard.

declare @criteria table(text1 varchar(20)) 
declare @searchtable table(longertext varchar(2000)) 

--lines wanted  
declare @linefrom int
declare @lineto int 

set @linefrom = 1
set @lineto = 2

insert @criteria values('asp.') 
insert @criteria values('ruby') 

insert @searchtable values('bla bla ruby') 
insert @searchtable values('bla asp. bla') 
insert @searchtable values('ruby asp. bla') 

;with a as ( 
select s.longertext, rn = row_number() over(order by longertext) from @searchtable s join @criteria c 
on s.longertext like '%'+ c.text1+'%' 
group by s.longertext 
) 
select longertext from a 
where rn between @linefrom and @lineto

Result:

bla asp. bla
bla bla ruby
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜