日期:2014-05-18  浏览次数:20398 次

如果判断一串ID是否在表中
假设表如下
ID text
1 ....
2 ....
4 ....
7 ....
10 ....
.......
n ....
  
检查id 字符串 "1,2,4" 这个在表中 那么返回 1
如果检查的字符串是 "2,3" ,因为3不在表中那么返回0

------解决方案--------------------
SQL code
     charindex('1,2,4',字段)>0来判断 return 0 else return 1

------解决方案--------------------
SQL code

create table t1(id int)
insert into t1
select 1 union all
select 2 union all
select 4
go

--都在
if exists (select 1 from t1 where charindex(','+ltrim(id)+',',','+'1,2,4'+',') = 0)
print 0
else
print 1

--某个不在
if exists (select 1 from t1 where charindex(','+ltrim(id)+',',','+'1,2,3'+',') = 0)
print 0
else
print 1

drop table t1

/*******************

1
0