日期:2014-05-18 浏览次数:20754 次
declare @sql varchar(8000) set @sql = 'select fcustomer ' select @sql = @sql + ' , max(case fperiod when ''' + ltrim(fperiod) + ''' then fremainamount else 0 end) [' + ltrim(fperiod) + ']' from (select distinct fperiod from yin5) as a set @sql = @sql + ' , sum(fremainamount) 总分 from yin5 group by fcustomer ' exec(@sql)
------解决方案--------------------
整型数值不能直接与字符串相加,需要把整型转为字符型,ltrim()函数是一个去除字符串左空格函数,利用它也可以把整型转为字符型
------解决方案--------------------
树哥正解,昨天我也遇到的!
------解决方案--------------------
declare @sql varchar(8000)
set @sql = 'select fcustomer '
select @sql = @sql + ' , max(case fperiod when ''' + fperiod + ''' then fremainamount else ''0'' end) [' + fperiod + ']'
from (select distinct fperiod from yin5) as a
set @sql = @sql + ' , sum(fremainamount) 总分 from yin5 group by fcustomer '
exec(@sql)