我创建了两个表学生和年级。学生表包含 id(PK)、姓名、分数、地址等列。我向其中插入了 10 个值。成绩表中有两个字段stud_id(外键)和stud_status。在年级中,我必须在存储过程中编写一个游标,以从学生表插入成绩表。条件类似于,如果学生成绩在成绩表中高于 50 分,则应在 Stud_status 中将其存储为“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:
but another error is showing "Error Code: 1366. Incorrect integer value: 'G' for column 's_stud_mark' at row 11
这是因为,您在表中将 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';
...
关于stored-procedures - 使用存储过程中的游标将表从一个表插入到另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20700027/