use tempdeclare @sql varchar(8000)
set @sql = 'select a.stu_id,a.stu_name,'
select @sql = @sql + 'sum(case cou_id when '''+cou_id+''' 
                          then score else null end) as '''+cou_id+''','
from (select distinct cou_id from selcou_info) as cset @sql = left(@sql,len(@sql)-1) + ' from stu_info a inner join selcou_info b on a.stu_id=b.stu_id group by a.stu_id,a.stu_name'exec(@sql)
go