我有某个类似这样字符串 string="2,3"数据库中有张表中的某个字段 对应了字符串里面的值  比如 Table A  
                                                     ID
                                                ----------
                                                      2
                                                      3
                                                      5
                                                      6 
如何通过sql语句把第一,第二条数据查出来?
                       

解决方案 »

  1.   

    select top 2 from A
      

  2.   

    select * from 表 where charindex(','+rtrim(ID)+',' , ','+@str+',')>0
      

  3.   

    我有某个类似这样字符串 string="2,3" 数据库中有张表中的某个字段 对应了字符串里面的值  比如 Table A   
                                                         ID 
                                                    ---------- 
                                                          2 
                                                          3 
                                                          5 
                                                          6  
    如何通过sql语句把第一,第二条数据查出来? 
    -------------------------------------------------------------------
    --使用时删除引号间的空格.
    declare @string as varchar(10)
    set @string = '2,3'select * from A where charindex(','+id+',' , ',' + @string + ',') > 0
      

  4.   

    ....忘记说了 如果我是Access库呢...-_-!!
      

  5.   

    select * from tablea where charindex(','+id+',',','+@string+',')>0
      

  6.   

    ....忘记说了 如果我是Access库呢...-_-!!
      

  7.   

    使用charindex函数。
    select * from 表 where charindex( ', '+rtrim(ID)+ ', ' ,  ', '+@str+ ', ') >0
      

  8.   

    declare @string as nvarchar(4000)
    set @string='2,3'
    declare @sql as nvarchar(400)set @sql='
    SELECT *
    FROM A
    where ID in('+@string+')'
    exec (@sql)