首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

帮忙翻译下SQL语句解决方案

2012-02-08 
帮忙翻译下SQL语句declare@sqlvarchar(8000)set@sqlselectstuidas学号,stunameas姓名select@sql@sql+,ma

帮忙翻译下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)

热点排行