如何在存储过程中更改某一字段的数据类型?

vegetablebird_ 2008-04-16 11:47:48
假如有一个表Choices表示所有学生所选的课程的成绩,其中记录是sid(学号)cid(课程号)以及score(sid所选的cid的成绩),现在要求写一个存储过程实现score由百分制向等级制的转换(即变成A、B、C、D、E),原来的score是int类型,要求转换后为char类型,请教各位如何实现?如果您给的是源代码,请做一下注释。谢谢!
...全文
273 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
bqb 2008-04-17
  • 打赏
  • 举报
回复
select sid,cid,score=case when score>=90 then 'A'  
when score>=80 and score<90 then 'B'
when score>=70 and score<80 then 'C'
when score>=60 and score<70 then 'D'
when score<60 then 'E' end
into Choices_tmp from Choices

drop table Choices

sp_rename 'Choices_tmp','Choices'
vegetablebird_ 2008-04-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bqb 的回复:]
SQL codeselect sid,cid,score=case when score>=90 then 'A'
when score>=80 and score<90 then 'B'
when score>=70 and score<80 then 'C'
when score>=60 and score<70 then 'D'
when score<60 then 'E' end
into Choices_tmp from Choices

drop table Choices

sp_rename 'Choices_tmp','Choices'
[/Quote]
要这样做,必须在存储过程中创建表Choices_tmp,但是方法中不是不可以使用Create语句么?
我原来的想法是可以在表Choices中添加新列score_tmp char(1),然后根据score的值像bqb
的方法一样去更改score_tmp,完成后drop score并sp_rename 'score_tmp','score','COLUMN',
这样子做之后执行时会报错说"列score_tmp是无效的".
如果是在存储过程之外创建表Choices_tmp,并在过程中修改,当然可以.不过这样子每次调用时都得由
用户自己新建一个表交给过程去修改,好像不太符合过程的定义原则.
所以我想问:有没有方法可以在一个过程中完成这件事?用户调用这个过程后就可以做到对分数的分级,而
不用自己手动创建另一张表.

34,872

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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