字符串:'aaa,bbb,ccc,123,456'
表:
f1 f2
1  aaa
2  bbb 
3  ccc
4  ddd如何可以判斷字符串中的某一元素如123,456在表中的F2列不存在,只要判斷字符串存不存在表中的列就行了,謝謝!
如果能指明123和456在表中不存在更好。

解决方案 »

  1.   

    select * from tb where 
    charindex(','+f2+',',','+字符串+',')>0
      

  2.   


    select * from tab
    where charindex(','+f2+',',','+'aaa,bbb,ccc,123,456'+',')>0
      

  3.   

    create table tb(f1 int,f2 varchar(50))
    insert into tb select 1,'aaa'
    insert into tb select 2,'bbb'
    insert into tb select 3,'ccc'
    insert into tb select 4,'ddd'
    declare @s varchar(8000)
    set @s='aaa,bbb,ccc,123,456'
    set @s='select '''+replace(@s,',',''' as id union select ''')+''''
    exec('select id from ('+@s +')t where id not in(select f2 from tb)')123
    456