SQL动态行列变换

来源:互联网 发布:淘宝网短裙 编辑:程序博客网 时间:2024/05/26 19:16
DECLARE @sql varchar(8000)
SELECT @sql=isnull(@sql+',','')+'max(case cname when '''+cname+''' then '''+degree+''' else 0 end) ['+cname+']'  //动态行列变换
from(
select * from (select * from (select a.stuid,a.classid,a.id,a.degree,a.cid,c.cname from (select b.stuid,a.classid,a.id,b.degree,a.cid from T_couser a left join sc b on a.id=b.cid) a left join course c on a.cid=c.cid)
a, T_stu b where a.stuid=b.sid) a where a.classid='001' or a.sid='001' or a.sname='001'
) AS a
SET @sql='select sid as 学号,sname as 姓名,'+@sql+' from  '
exec(@sql)
1 0
原创粉丝点击