首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-03-26 
如果判断一串ID是否在表中假设表如下IDtext1....2....4....7....10...........n....检查id 字符串1,2,4

如果判断一串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 

热点排行