declare @val varchar(1000);select top 1 @val=stuff((select ','+cast(sfjlbh as varchar(10)) from sf_sfjl where jfsj>='2010-11-01' and jfsj<='2010-11-30'  for xml path('')), 1, 1, '')  from sf_sfjl 上面的sql语句应该是这样的串 @val='1,5,6,7'
 
但是下面我就要用到@val  :select * from 表 where id in(@val)重点就在这里:@val 是个字符串  而in()要的是 in(1,5,6,7),这个问题怎么样处理才好? 高手帮帮忙!

解决方案 »

  1.   

    楼主的意思是多两个单引号吗?使用replace替换掉不就成了
      

  2.   


    不是这样的  in() 要的是int
    而@val 是字符串 
      

  3.   

    修改sql语句.
    eg:
    select * from 表 where id in(select [id] from 表 where [createtime]<getdate())
      

  4.   

    decalare @strSql varchar(8000)
    @strSql='select * from 表 where id in('+@val+')'
    exec(@strSql)
      

  5.   

    不管是int 还是varchar 只要将SQL语句拼成select * from 表 where id in (1,5,6,7)这种样式就可以执行成功啊!~
      

  6.   

    这里不应该用字符串,应该用一个集合。select * from 表 where id in(select ddt from sf_sfjl where jfsj>='2010-11-01' and jfsj<='2010-11-30')或者
    with abc as
    {
      select ddt from sf_sfjl where jfsj>='2010-11-01' and jfsj<='2010-11-30'
    }
    select * from 表 where id in( select ddt from abc)列的名称相不相同无所谓,关键是类型要对
      

  7.   


    表值函数 用不了 exec方法   ,但是还是谢谢你