日期:2014-05-19  浏览次数:20653 次

帮忙翻译下SQL语句
declare   @sql   varchar(8000)
set   @sql   =   'select   stuid   as   学号,stuname   as   姓名 '
select   @sql   =   @sql   +   '   ,   max(case   lessionname   when   ' ' '   +   lessionname   +   ' ' '   then   score   else   null   end)  
[ '   +   lessionname   +   '] '
from   (select   distinct   lessionname   from   (select   a.stuid   ,   b.stuname   ,   c.lessionname   ,  
a.score   from   stu_lession   c,   stu_profile   b,   stu_score   a   where   a.stuid   =   b.stuid   and   a.lessionid   =   c.lessionid)   t   )
as   a
set   @sql   =   @sql   +   '   from   (select   a.stuid   ,   b.stuname   ,   c.lessionname   ,   a.score   from   stu_lession   c,  
stu_profile   b,   stu_score   a   where   a.stuid   =   b.stuid   and   a.lessionid   =   c.lessionid)   t   group   by   stuid,stuname '
exec(@sql)  

翻译成一个完整的SQL语句,就是不用@SQL   直接写那种

------解决方案--------------------
declare @sql varchar(8000)
set @sql = 'select stuid as 学号,stuname as 姓名 '
select @sql = @sql + ' , max(case lessionname when ' ' ' + lessionname + ' ' ' then score else null end)
[ ' + lessionname + '] '
from (select distinct lessionname from (select a.stuid , b.stuname , c.lessionname ,
a.score from stu_lession c, stu_profile b, stu_score a where a.stuid = b.stuid and a.lessionid = c.lessionid) t )
as a
set @sql = @sql + ' from (select a.stuid , b.stuname , c.lessionname , a.score from stu_lession c,
stu_profile b, stu_score a where a.stuid = b.stuid and a.lessionid = c.lessionid) t group by stuid,stuname '

print @sql --------------加这个你就可以看到的了!


exec(@sql)