set @tmp='select '+@OtherName+' = '+@OtherName+'+name
        from others where otherId in ('+@OtherId+')'
exec(@sql)

解决方案 »

  1.   

    还是不行! 难点是在累积字串字量@OtherName, 如果采用exec(sql)这种方式就有问题!
      

  2.   

    set @tmp='set @other='' select @Other = @Other+name
            from others where otherId in ('+@OtherId+')'
    exec sp_executesql @tmp,N'@other varchar(500) output',@othername  output
      

  3.   

    上面错了!
    set @tmp='set @other='''' select @Other = @Other+name
            from others where otherId in ('+@OtherId+')'
    exec sp_executesql @tmp,N'@other varchar(500) output',@othername  output