charindex()、like示例:
/*
时间:2012-02-24
作者:方方
*/
if object_id('t','u') is not null drop table t
go
create table t(uid int,prods varchar(10))
insert into t select 1,'a,b,c,d'
union all select 2,'a,c,d,f'
union all select 3,'d,f'
go
declare @case varchar(20)
set @case='c,a'
--你的方法
select * from t where prods like '%'+REPLACE(@case,',','%')+'%'
--下面是改后的
declare @sql nvarchar(100)
set @sql='''%'+REPLACE(@case,',','%'' and prods like ''%')+'%''';
exec('select * from t where prods like '+@sql);
set @sql=REPLACE(@case,',',''',prods)>0 and charindex(''');
exec('select * from t where charindex('''+@sql+''',prods)>0');
drop table t