当前位置: 代码迷 >> Sql Server >> 如果判断一串ID是否在表中,该如何处理
  详细解决方案

如果判断一串ID是否在表中,该如何处理

热度:72   发布时间:2016-04-27 14:04:05.0
如果判断一串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 t1select 1 union allselect 2 union allselect 4go--都在if exists (select 1 from t1 where charindex(','+ltrim(id)+',',','+'1,2,4'+',') = 0)print 0elseprint 1--某个不在if exists (select 1 from t1 where charindex(','+ltrim(id)+',',','+'1,2,3'+',') = 0)print 0elseprint 1drop table t1/*******************10
  相关解决方案