2013-12-20 40 views
1

我创建了两个表格学生和等级。学生表包含id(PK),名称,标记,地址的列。我给它插入了10个值。在等级表中有两个coulmns stud_id(外键)和stud_status。在等级上我必须在存储过程中写入一个光标,以便从学生表中插入成绩表。条件就像是如果学生的成绩在成绩表中高于50,它应该在stud_status中存储为'G',对于它,也应该存储为stud_id。如果mark = 50,它应该存储'E',否则是'L' 我使用下面的代码。和iam使用MySQL Workbench 6.0。使用存储过程中的光标从一个插入到另一个表

use test; 
    delimiter $$ 
    drop procedure if exists `p_status` $$ 
    create procedure `p_status`() 
     begin 
     declare s_stud_mark int(111); 
     declare s_stud_id int(111); 
     declare cur_stud cursor For Select stud_id,stud_mark from student where stud_id    is not null; 
     open cur_stud; 
      fetch cur_stud into s_stud_mark,s_stud_id; 
     if(stud_mark > 50) then 
     set s_stud_mark='G'; 
      insert into grade(`stud_id`,`stud_staus`)values(s_stud_id,s_stud_mark); 
      else if(stud_mark = 50) then 
     set s_stud_mark='E'; 
      insert into grade(`stud_id`,`stud_status`) values(s_stud_id,s_stud_mark); 
      else 
      set s_stud_mark='L'; 
      insert into grade(`stud_id`,`stud_status`)values(s_stud_id,s_stud_mark); 
      end if ; 
      end if ; 
     close cur_stud; 
     end $$ 
     delimiter ; 

,但它显示的错误为 “错误代码:在 '字段列表' 1054年未知列 'stud_mark'”

有人回复

回答

2

错误是在线路:

if(stud_mark > 50) then 
... 
else if(stud_mark = 50) then 

将它们更改为:

if(s_stud_mark > 50) then 
... 
else if(s_stud_mark = 50) then 

更新1

但另一误差是显示“错误代码:1366不正确的整数值: 'G' 列 's_stud_mark' 在列11

这是因为您在表格中定义了stud_markint,但您在例程中将char分配给它。实际上,您应该在例程中定义一个变量s_stud_status并为其分配值,如set s_stud_status='G';
类似于例程中的其他等级值。

并根据需要更改代码如下。

if(s_stud_mark > 50) then 
    set s_stud_status='G'; 
    insert into grade(`stud_id`,`stud_status`) values(s_stud_id,s_stud_status); 
else ... 
    set s_stud_status='E'; 
    ... 
+0

感谢代码运行成功。 – Benny

相关问题