首先,您需要一个拆分功能:
CREATE function [dbo].[f_split]
(
@param nvarchar(max),
@delimiter char(1)
)
returns @t table (val nvarchar(max), seq int)
as
begin
set @param += @delimiter
;with a as
(
select cast(1 as bigint) f, charindex(@delimiter, @param) t, 1 seq
union all
select t + 1, charindex(@delimiter, @param, t + 1), seq + 1
from a
where charindex(@delimiter, @param, t + 1) > 0
)
insert @t
select substring(@param, f, t - f), seq from a
option (maxrecursion 0)
return
end
然后你可以像这样搜索:
-- declaring a tablevariable to represent your table
declare @yourtable table(id int identity(1,1), searchcol varchar(50))
insert @yourtable values('abc')
insert @yourtable values('za')
insert @yourtable values('az')
insert @yourtable values('zz')
declare @input varchar(50)
set @input = 'a b c'
-- show if one or more match exists
select * from @yourtable a
where exists (select 1 from f_split(@input, ' ') b
where a.searchcol like '%'+ b.val + '%')
--show only if all matches exists
select * from @yourtable a
where not exists (select 1 from clausens_base.dbo.f_split(@input, ' ') b
where not a.searchcol like '%'+ b.val + '%')