简单来说,就是有一列存有字符串,是以","分隔的,比如1,2,3,4,5,6,7,8,9,10
现在传递进来一参数,怎么判断这个参数是否在这些字符里面,比如1就存在,11就不存在。
要能找到所有行,意思就是这个判断要能放到where字句中。
------解决方案--------------------
DECLARE @a CHAR(10)='1'
SELECT * FROM tablename WHERE CHARINDEX(','+@a+',',','+columnname+',')>0
------解决方案--------------------
-- 建测试表
create table #t(s varchar(100))
insert into #t(s)
select '1,2,3,4,5,6,7,8,9,10'
-- 测试1
declare @x varchar(10)
select @x='1'
if exists(select 1 from #t where charindex(','+@x+',',','+s+',',1)>0)
print '存在'
else
print '不存在'
--> 结果: 存在
-- 测试2
declare @x varchar(10)
select @x='11'
if exists(select 1 from #t where charindex(','+@x+',',','+s+',',1)>0)
print '存在'
else
print '不存在'
--> 结果: 不存在
-- 测试3
declare @x varchar(10)
select @x='5'
if exists(select 1 from #t where charindex(','+@x+',',','+s+',',1)>0)
print '存在'
else
print '不存在'
--> 结果: 存在