declare @sql varchar(8000)
set @mth='('+ @mth +')'
set @dep='('+ @dep +')'
set @sql='
select mth,compid,depname,sum(mtotal) as total from call_list
where Mth in' + @mth + 'and depname in  (' + @dep + ')
group by mth,compid,depname'
我这里的@mth 和 @dep 都是字符串来的,这样查出来的sql语句变量里面是没有引号的
要怎么把这个引号加上啊?

解决方案 »

  1.   


    set @sql='
    select mth,compid,depname,sum(mtotal) as total from call_list
    where Mth in''' + @mth + '''and depname in  (''' + @dep + ''')
    group by mth,compid,depname'
      

  2.   


    declare @sql varchar(8000)
    set @mth='('+ @mth +')'
    set @dep='('+ @dep +')'
    set @sql='
    select mth,compid,depname,sum(mtotal) as total from call_list
    where Mth in ''' + @mth + ''' and depname in  (''' + @dep + ''')
    group by mth,compid,depname'
      

  3.   

    set @mth='('+ replace(@mth,'''','''''') +')'
    set @dep='('+ replace(@dep,'''','''''') +')'
      

  4.   

    如果@mth,@dep 是类似这样的:1,2,3  则如下declare @sql varchar(8000)
    set @mth='1,2,3'
    set @dep='4,5,6'   --不要加括号了
    set @sql='
    select mth,compid,depname,sum(mtotal) as total from call_list
    where Mth in (' + @mth + ') and depname in  (' + @dep + ')
    group by mth,compid,depname'