Mysql 使用存储过程中的游标从一个表插入到另一个表中

Mysql 使用存储过程中的游标从一个表插入到另一个表中,mysql,stored-procedures,cursor,mysqldump,mysql-workbench,Mysql,Stored Procedures,Cursor,Mysqldump,Mysql Workbench,我创建了两个表,分别是学生和年级。学生表包含id(PK)、name、mark和address列。我给它插入了10个值。成绩表中有两个coulmns stud_id(外键)和stud_status。在年级中,我必须在存储过程中写入一个游标,从学生表插入到年级表中。如果成绩表中的学生分数超过50,则该条件将类似于,它应存储为stud_状态中的“G”,并且该stud_id也应存储为“G”。如果标记=50,则应存储“E”,否则为“L” 我使用下面的代码。和iam使用MySQL Workbench 6.0

我创建了两个表,分别是学生和年级。学生表包含id(PK)、name、mark和address列。我给它插入了10个值。成绩表中有两个coulmns stud_id(外键)和stud_status。在年级中,我必须在存储过程中写入一个游标,从学生表插入到年级表中。如果成绩表中的学生分数超过50,则该条件将类似于,它应存储为stud_状态中的“G”,并且该stud_id也应存储为“G”。如果标记=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’”


任何人回复

错误都在以下行中:

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。第11行的列“s_stud_mark”的整数值“G”不正确

这是因为,您在表中定义了
stud_mark
int
,但在例程中为其分配了
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';
  ...