select * from authors where au_id in ('172-32-1176','238-95-7766','341-22-1782')

解决方案 »

  1.   


    declare @tmp varchar(500)
    set @tmp='''172-32-1176'',''238-95-7766'',''341-22-1782'''
    exec('select * from authors where au_id in ('+@tmp+')')
      

  2.   

    set @tmp='''172-32-1176'',''238-95-7766'',''341-22-1782'
    有这样赋值的吗?一个变量好象不可以赋予多个值吧?况且set @tmp='''172-32-1176'',''238-95-7766'',''341-22-1782''应该是只给@tmp赋了一个值!请高手指教!
      

  3.   

    动态语句要 exec('select * from authors where au_id in ('+@tmp+')')
      

  4.   

    我也遇到过类似问题,我个人认为sql在后台把set @tmp='''172-32-1176'',''238-95-7766'',''341-22-1782'''
    select * from authors where au_id in (@tmp)当成了select * from authors where au_id = '''172-32-1176'',''238-95-7766'',''341-22-1782'''
    --这个句子只是示意,他本身有语法错误但是,我没有办法去证实,希望大虾指点。可以这样变通:use pubs
    declare @tmp varchar(500)
    set @tmp='''172-32-1176'',''238-95-7766'',''341-22-1782'''
    exec('select * from authors where au_id in ('+@tmp+')')
      

  5.   

    use pubs
    declare @tmp varchar(500),@sql varchar(2000)
    set @tmp='''172-32-1176'',''238-95-7766'',''341-22-1782'''
    set @sql='select * from authors where au_id in ('+@tmp+')'
    exec(@sql)