假设有表A 中某个字段a中的内容如下:a,b,c
a,b
b,c
c,d
c,a
c,b,d
....
现在给出一个参数 参数内容可能是 a,b,c,d 中的一个或多个,
希望可以搜出 包含其中任意一个的记录,也就是假设 现在参数内容为 (a,b),那结果集应该是字段a中包含a或包含b的结果。
a,b
b,c
c,d
c,a
c,b,d
....
现在给出一个参数 参数内容可能是 a,b,c,d 中的一个或多个,
希望可以搜出 包含其中任意一个的记录,也就是假设 现在参数内容为 (a,b),那结果集应该是字段a中包含a或包含b的结果。
set @str='1,2,12,34'
set @str='insert into #test(number) select '+REPLACE(@str,',',' union all select ')
--print @str
create table #test
(
id int identity,
number int
)
exec(@str)
select
a.number
from master..spt_values a
inner join #test b
on CHARINDEX(ltrim(b.number),ltrim(a.number))>0
where a.type='p' and a.number between 1 and 100
/*
number
-----------
1
10
11
12
13
14
15
16
17
18
19
21
31
41
51
61
71
81
91
100
2
12
20
21
22
23
24
25
26
27
28
29
32
42
52
62
72
82
92
12
34(41 行受影响)
*/
关键是参数是别人选的 我把所有的可能一一列出来,如上面的情况 我可能列的是 a,b,c,d,e.然后用户可能选一个多个。又不会是一个固定的情况
gei l wo lingwai de silu xiexie