declare @sql varchar(4000) set @sql='select name' select @sql=@sql+',isnull(sum(case subject when '''+subject+''' then result end),0)['+subject+']' from (select distinct subject from CJ) as a select @sql=@sql+' from CJ group by Name' print @sql exec(@sql)
参考:http://www.cnblogs.com/iamzhanglei/archive/2009/12/27/1633356.html