为什么这个存储过程会错呢????拜求!!!

hanzhuang639 2006-05-09 10:43:57

-------------------------------------------------单科成绩细化表

CREATE proc dkcjxh(@subjectid int)
as

select rrt_examclass,rrt_studentid,rrt_mark into # from student where rrt_examsubjectid=@subjectid order by rrt_examclass,rrt_studentid
--select * from #
--drop table #

declare @sql0 nvarchar(4000),@sql1 nvarchar(4000),@sql2 nvarchar(4000),@sql3 nvarchar(4000),@sql4 nvarchar(4000),@sql5 nvarchar(4000),@sql6 nvarchar(4000),@sql7 nvarchar(4000),@sql8 nvarchar(4000),@sql9 nvarchar(4000),@sql10 nvarchar(4000)

set @sql0=''
set @sql1=''
set @sql2=''
set @sql3=''
set @sql4=''
set @sql5=''
set @sql6=''
set @sql7=''
set @sql8=''
set @sql9=''
set @sql10=''
select @sql0=@sql0+',cast(sum(case when rrt_examclass='+cast(rrt_examclass as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where rrt_examclass='+cast(rrt_examclass as varchar(10))+' and rrt_mark>=140) as nvarchar) as ['+cast(rrt_examclass as varchar(10))+'班]'
,@sql1=@sql1+',cast(sum(case when rrt_examclass='+cast(rrt_examclass as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where rrt_examclass='+cast(rrt_examclass as varchar(10))+' and rrt_mark>=130) as nvarchar) as ['+cast(rrt_examclass as varchar(10))+'班]'
,@sql2=@sql2+',cast(sum(case when rrt_examclass='+cast(rrt_examclass as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where rrt_examclass='+cast(rrt_examclass as varchar(10))+' and rrt_mark>=120) as nvarchar) as ['+cast(rrt_examclass as varchar(10))+'班]'
,@sql3=@sql3+',cast(sum(case when rrt_examclass='+cast(rrt_examclass as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where rrt_examclass='+cast(rrt_examclass as varchar(10))+' and rrt_mark>=110) as nvarchar) as ['+cast(rrt_examclass as varchar(10))+'班]'
,@sql4=@sql4+',cast(sum(case when rrt_examclass='+cast(rrt_examclass as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where rrt_examclass='+cast(rrt_examclass as varchar(10))+' and rrt_mark>=100) as nvarchar) as ['+cast(rrt_examclass as varchar(10))+'班]'
,@sql5=@sql5+',cast(sum(case when rrt_examclass='+cast(rrt_examclass as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where rrt_examclass='+cast(rrt_examclass as varchar(10))+' and rrt_mark>=90) as nvarchar) as ['+cast(rrt_examclass as varchar(10))+'班]'
,@sql6=@sql6+',cast(sum(case when rrt_examclass='+cast(rrt_examclass as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where rrt_examclass='+cast(rrt_examclass as varchar(10))+' and rrt_mark>=80) as nvarchar) as ['+cast(rrt_examclass as varchar(10))+'班]'
,@sql7=@sql7+',cast(sum(case when rrt_examclass='+cast(rrt_examclass as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where rrt_examclass='+cast(rrt_examclass as varchar(10))+' and rrt_mark>=70) as nvarchar) as ['+cast(rrt_examclass as varchar(10))+'班]'
,@sql8=@sql8+',cast(sum(case when rrt_examclass='+cast(rrt_examclass as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where rrt_examclass='+cast(rrt_examclass as varchar(10))+' and rrt_mark>=60) as nvarchar) as ['+cast(rrt_examclass as varchar(10))+'班]'
,@sql9=@sql9+',cast(sum(case when rrt_examclass='+cast(rrt_examclass as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where rrt_examclass='+cast(rrt_examclass as varchar(10))+' and rrt_mark>=50) as nvarchar) as ['+cast(rrt_examclass as varchar(10))+'班]'
,@sql10=@sql10+',cast(sum(case when rrt_examclass='+cast(rrt_examclass as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where rrt_examclass='+cast(rrt_examclass as varchar(10))+' and rrt_mark>=0) as nvarchar) as ['+cast(rrt_examclass as varchar(10))+'班]' from # group by rrt_examclass order by rrt_examclass


exec ('
select ''140-150'' as 分数段'+@sql0+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where rrt_mark>=140) as nvarchar) as [年段]
from #
where rrt_mark>=140
union all
select ''130-139'' as 分数段'+@sql1+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where rrt_mark>=130) as nvarchar) as [年段]
from #
where rrt_mark<140 and rrt_mark>=130
union all
select ''120-129'' as 分数段'+@sql2+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where rrt_mark>=120) as nvarchar) as [年段]
from #
where rrt_mark<130 and rrt_mark>=120
union all
select ''110-119'' as 分数段'+@sql3+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where rrt_mark>=110) as nvarchar) as [年段]
from #
where rrt_mark<120 and rrt_mark>=110
union all
select ''100-109'' as 分数段'+@sql4+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where rrt_mark>=100) as nvarchar) as [年段]
from #
where rrt_mark<110 and rrt_mark>=100
union all
select ''90-99'' as 分数段'+@sql5+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where rrt_mark>=90) as nvarchar) as [年段]
from #
where rrt_mark<100 and rrt_mark>=90
union all
select ''80-89'' as 分数段'+@sql6+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where rrt_mark>=80) as nvarchar) as [年段]
from #
where rrt_mark<90 and rrt_mark>=80
union all
select ''70-79'' as 分数段'+@sql7+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where rrt_mark>=70) as nvarchar) as [年段]
from #
where rrt_mark<80 and rrt_mark>=70
union all
select ''60-69'' as 分数段'+@sql8+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where rrt_mark>=60) as nvarchar) as [年段]
from #
where rrt_mark<70 and rrt_mark>=60
union all
select ''50-59'' as 分数段'+@sql9+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where rrt_mark>=50) as nvarchar) as [年段]
from #
where rrt_mark<60 and rrt_mark>=50
union all
select ''0-49'' as 分数段'+@sql10+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where rrt_mark>=0) as nvarchar) as [年段]
from #
where rrt_mark<50
')

drop table #
GO

请各位好心人有耐心的帮我看看好吗?在这里拜求!!!!
...全文
143 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
hanzhuang639 2006-05-09
  • 打赏
  • 举报
回复
大哥别顶了!给点实际的水啊!
sxdoujg 2006-05-09
  • 打赏
  • 举报
回复
up
hanzhuang639 2006-05-09
  • 打赏
  • 举报
回复
上面的代码是我改过了的!我现在贴一个原来的给你们看看!
-------------------------------------------------单科成绩细化表

CREATE proc dkcjxh(@subjectid int)
as

select classid,sc.stuid,score into # from sc,stu where sc.stuid=stu.stuid and subjectid=@subjectid order by classid,sc.stuid
--select * from #
--drop table #

declare @sql0 nvarchar(4000),@sql1 nvarchar(4000),@sql2 nvarchar(4000),@sql3 nvarchar(4000),@sql4 nvarchar(4000),@sql5 nvarchar(4000),@sql6 nvarchar(4000),@sql7 nvarchar(4000),@sql8 nvarchar(4000),@sql9 nvarchar(4000),@sql10 nvarchar(4000)

set @sql0=''
set @sql1=''
set @sql2=''
set @sql3=''
set @sql4=''
set @sql5=''
set @sql6=''
set @sql7=''
set @sql8=''
set @sql9=''
set @sql10=''
select @sql0=@sql0+',cast(sum(case when classid='+cast(classid as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where classid='+cast(classid as varchar(10))+' and score>=140) as nvarchar) as ['+cast(classid as varchar(10))+'班]'
,@sql1=@sql1+',cast(sum(case when classid='+cast(classid as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where classid='+cast(classid as varchar(10))+' and score>=130) as nvarchar) as ['+cast(classid as varchar(10))+'班]'
,@sql2=@sql2+',cast(sum(case when classid='+cast(classid as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where classid='+cast(classid as varchar(10))+' and score>=120) as nvarchar) as ['+cast(classid as varchar(10))+'班]'
,@sql3=@sql3+',cast(sum(case when classid='+cast(classid as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where classid='+cast(classid as varchar(10))+' and score>=110) as nvarchar) as ['+cast(classid as varchar(10))+'班]'
,@sql4=@sql4+',cast(sum(case when classid='+cast(classid as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where classid='+cast(classid as varchar(10))+' and score>=100) as nvarchar) as ['+cast(classid as varchar(10))+'班]'
,@sql5=@sql5+',cast(sum(case when classid='+cast(classid as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where classid='+cast(classid as varchar(10))+' and score>=90) as nvarchar) as ['+cast(classid as varchar(10))+'班]'
,@sql6=@sql6+',cast(sum(case when classid='+cast(classid as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where classid='+cast(classid as varchar(10))+' and score>=80) as nvarchar) as ['+cast(classid as varchar(10))+'班]'
,@sql7=@sql7+',cast(sum(case when classid='+cast(classid as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where classid='+cast(classid as varchar(10))+' and score>=70) as nvarchar) as ['+cast(classid as varchar(10))+'班]'
,@sql8=@sql8+',cast(sum(case when classid='+cast(classid as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where classid='+cast(classid as varchar(10))+' and score>=60) as nvarchar) as ['+cast(classid as varchar(10))+'班]'
,@sql9=@sql9+',cast(sum(case when classid='+cast(classid as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where classid='+cast(classid as varchar(10))+' and score>=50) as nvarchar) as ['+cast(classid as varchar(10))+'班]'
,@sql10=@sql10+',cast(sum(case when classid='+cast(classid as varchar(10))+' then 1 else 0 end) as nvarchar)+''/''+cast((select count(*) from # where classid='+cast(classid as varchar(10))+' and score>=0) as nvarchar) as ['+cast(classid as varchar(10))+'班]' from # group by classid order by classid


exec ('
select ''140-150'' as 分数段'+@sql0+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where score>=140) as nvarchar) as [年段]
from #
where score>=140
union all
select ''130-139'' as 分数段'+@sql1+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where score>=130) as nvarchar) as [年段]
from #
where score<140 and score>=130
union all
select ''120-129'' as 分数段'+@sql2+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where score>=120) as nvarchar) as [年段]
from #
where score<130 and score>=120
union all
select ''110-119'' as 分数段'+@sql3+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where score>=110) as nvarchar) as [年段]
from #
where score<120 and score>=110
union all
select ''100-109'' as 分数段'+@sql4+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where score>=100) as nvarchar) as [年段]
from #
where score<110 and score>=100
union all
select ''90-99'' as 分数段'+@sql5+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where score>=90) as nvarchar) as [年段]
from #
where score<100 and score>=90
union all
select ''80-89'' as 分数段'+@sql6+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where score>=80) as nvarchar) as [年段]
from #
where score<90 and score>=80
union all
select ''70-79'' as 分数段'+@sql7+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where score>=70) as nvarchar) as [年段]
from #
where score<80 and score>=70
union all
select ''60-69'' as 分数段'+@sql8+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where score>=60) as nvarchar) as [年段]
from #
where score<70 and score>=60
union all
select ''50-59'' as 分数段'+@sql9+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where score>=50) as nvarchar) as [年段]
from #
where score<60 and score>=50
union all
select ''0-49'' as 分数段'+@sql10+',cast(count(*) as nvarchar)+''/''+cast((select count(*) from # where score>=0) as nvarchar) as [年段]
from #
where score<50
')

drop table #
GO
hanzhuang639 2006-05-09
  • 打赏
  • 举报
回复
不会吧!
昵称被占用了 2006-05-09
  • 打赏
  • 举报
回复
你贴出的代码也没找到中文的
昵称被占用了 2006-05-09
  • 打赏
  • 举报
回复
提示是
第 2 行: '(' 附近有语法错误。
这个括号是中文的

hanzhuang639 2006-05-09
  • 打赏
  • 举报
回复
中文!不会啊!我没有用中文的'('哦!这个我还是知道的!从语法上没有错误的!有可能是逻辑上的错吧!因为我都已经可以创建这个存储过程了!就是在调用的时候出错了!
昵称被占用了 2006-05-09
  • 打赏
  • 举报
回复
从提示看,两个问题:
1、用了中文( ,没找到。。。。。
2、一个语句case太多,需要用临时表分开做,不要这么多union all
hanzhuang639 2006-05-09
  • 打赏
  • 举报
回复
整个存储过程要实现成下面这样数据!
 分数段   1班   2班  年段
140-150         0/0
130-139         0/0
120-129         0/0
110-119         0/0
100-109         0/0
90-99    2/2   1/1   3/3
80-89    3/5   0/1   3/6
70-79          0/6
60-69    1/6   0/1   1/7
50-59  0/7
0-49  0/7
hanzhuang639 2006-05-09
  • 打赏
  • 举报
回复
报这样的错!

所影响的行数为 18 行)

服务器: 消息 170,级别 15,状态 1,行 2
第 2 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 2
第 2 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 2
第 2 行: '(' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,行 2
在关键字 'as' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 6
第 6 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 6
第 6 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 6
第 6 行: '(' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,行 6
在关键字 'as' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 10
第 10 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 10
第 10 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 10
第 10 行: '(' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,行 10
在关键字 'as' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 14
第 14 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 14
第 14 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 14
第 14 行: '(' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,行 14
在关键字 'as' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 18
第 18 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 18
第 18 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 18
第 18 行: '(' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,行 18
在关键字 'as' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 22
第 22 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 22
第 22 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 22
第 22 行: '(' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,行 22
在关键字 'as' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 26
第 26 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 26
第 26 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 26
第 26 行: '(' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,行 26
在关键字 'as' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 30
第 30 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 30
第 30 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 30
第 30 行: '(' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,行 30
在关键字 'as' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 34
第 34 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 34
第 34 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 34
第 34 行: '(' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,行 34
在关键字 'as' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 38
第 38 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 38
第 38 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 38
第 38 行: '(' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,行 38
在关键字 'as' 附近有语法错误。
服务器: 消息 125,级别 15,状态 1,行 42
Case 表达式只能嵌套到 10 层。
服务器: 消息 170,级别 15,状态 1,行 42
第 42 行: '(' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 42
第 42 行: '(' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,行 42
在关键字 'as' 附近有语法错误。
hanzhuang639 2006-05-09
  • 打赏
  • 举报
回复
我现在建立了这样的表 student
---------------------------------------------------------------------------------------
id |rrt_examsubjectid|rrt_examclass|rrt_studentid|rrt_student|rrt_examsubject|rrt_mark|
---------------------------------------------------------------------------------------
1 | 1 | 高一(1) | 001 | 张三 | 语文 | 85 |
---------------------------------------------------------------------------------------
2 | 1 | 高一(1) | 002 | 李四 | 语文 | 95 |
---------------------------------------------------------------------------------------
hyrongg 2006-05-09
  • 打赏
  • 举报
回复
报什么错?我在分析器里面
CTRL+F5没有问题呀
hyrongg 2006-05-09
  • 打赏
  • 举报
回复
把你里面用到的表结构贴出来帮你看

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧