呵,它把(''+@str+'') 给当作一个字符串了。
建议用Charindex(idl,@Str)>0
  select * from tt2 where Charindex(','+idl+',',@str)>0
 或
Exec('Select * from tt2 where idl in ('+@Str+')')

解决方案 »

  1.   

    exec('select * from tt2 where id1 in (' + @str + ')')
      

  2.   

    1.
    exec ('select * from tt2 where id1 in ('+@str+')')
    2.
    select * from tt2 where charindex('''+id1+''',@str) > 0
      

  3.   

    declare @a varchar(8000)
    set @a='''0110'',''0125'',''0223'',''0231'',''02580110h'',''012h5'',''022h3'',''0hh1'',''hhfd'''
    exec('select * from tt2 where id1 in ('+@str+')')
      

  4.   

    Exec('Select * from tt2 where idl in ('+@Str+')')
    能得到
    但用 select * from tt2 where Charindex(','+idl+',',@str)>0
    也得不到。 97866(weiLuang
    能在改一改吗?
    我不能用Exec('Select * from tt2 where idl in ('+@Str+')')
    因为程序中要用用执行的。
      

  5.   

    select * from tt2 where charindex('''+id1+''',@str) > 0
    也不能得到数据。
      

  6.   


    我实在搞不明白为什么用Select * from tt2 where id1 in ('+@Str1+')不行Exec('Select * from tt2 where idl in ('+@Str+')')
    而用这个行。为什么呀?
      

  7.   

    看一下 CHARINDEX 的语法:返回字符串中指定表达式的起始位置。 语法
    CHARINDEX ( expression1 , expression2 [ , start_location ] ) 参数
    expression1一个表达式,其中包含要寻找的字符的次序。expression1 是一个短字符数据类型分类的表达式。expression2一个表达式,通常是一个用于搜索指定序列的列。expression2 属于字符串数据类型分类。start_location在 expression2 中搜索 expression1 时的起始字符位置。如果没有给定 start_location,而是一个负数或零,则将从 expression2 的起始位置开始搜索。返回类型
    int注释
    如果 expression1 或 expression2 之一属于 Unicode 数据类型(nvarchar 或 nchar)而另一个不属于,则将另一个转换为 Unicode 数据类型。如果 expression1 或 expression2 之一为 NULL 值,则当数据库兼容级别为 70 或更大时,CHARINDEX 返回 NULL 值。当数据库兼容级别为 65 或更小时,CHARINDEX 仅在 expression1 和 expression2 都为 NULL 时返回 NULL 值。 如果在 expression2 内没有找到 expression1,则 CHARINDEX 返回 0。示例
    第一个代码示例返回序列"wonderful"在 titles 表的 notes 列中开始的位置。第二个示例使用可选的 start_location 参数从 notes 列的第五个字符开始寻找"wonderful"。第三个示例显示了当 expression2 内找不到 expression1 时的结果集。USE pubs
    GO
    SELECT CHARINDEX('wonderful', notes)
    FROM titles
    WHERE title_id = 'TC3218'
    GO-- Use the optional start_location parameter to start searching 
    -- for wonderful starting with the fifth character in the notes
    -- column.
    USE pubs
    GO
    SELECT CHARINDEX('wonderful', notes, 5)
    FROM titles
    WHERE title_id = 'TC3218'
    GO下面是第一个查询和第二个查询的结果集:----------- 
    46          (1 row(s) affected)USE pubs
    GO
    SELECT CHARINDEX('wondrous', notes)
    FROM titles
    WHERE title_id='TC3218'
    GO下面是结果集。
    ----------- 
    0          (1 row(s) affected
      

  8.   

    如果你的@Str为:
      Set @Str='''0110'',''0125'',''0223'',''0231'',''02580110h'',''012h5'',''022h3'''
      即 @Str为字串 '0110','0125','0223','0231','02580110h','012h5','022h3'
    则:
      select * from tt2 where charindex(','''+id1+''',',','+@str+',') > 0如果你的@Str为:
      Set @Str='0110,0125,0223,0231,02580110h,012h5,022h3'
      即 @Str为字串 '0110,0125,0223,0231,02580110h,012h5,022h3'
    则:
      select * from tt2 where charindex(','+id1+',',','+@str+',') > 0
      

  9.   

    SQL语句中不能使用变量,要使用变量,只能通过execute