Search code examples
mysqlstored-proceduresmysql-error-1241

mysterious runtime error in stored proc


DELIMITER //
drop procedure if exists stugrade//
create procedure stugrade()
begin
declare v_pid,v_sub1,v_sub2,v_sub3 int;
declare total int default 0;
declare v_finished int default 0;
declare per decimal(10,2);
declare stud_cur cursor for select sid,sub1,sub2,sub3 from student;
declare continue handler for not found set v_finished=1;
    set per=0;
open stud_cur;
label1:loop
fetch stud_cur into v_pid,v_sub1,v_sub2,v_sub3;
    if v_finished=1 then
    leave label1;
    end if;
set total=(v_sub1,v_sub2,v_sub3);
set per=total/3;
select concat(concat(concat('marks-->',v_sub1),v_sub2),v_sub3) as "subject wise marks";
select concat('total marks of student',total) as "total marks";
select concat('Percentage of student',per) as "percentage";
if(per>=66) then
    select concat('','Distinction') as "class";
elseif(per<66 and per>=60) then
    select concat('','first') as "class";
elseif(per<60 and per>=55) then
    select concat('','higher Second') as "class";
elseif(per<55 and per>=50) then
    select concat('','second') as "class";
elseif(per<50 and per>=40) then
    select concat('','pass') as "class";
else
    select concat('','fail') as "class";
end if;
end loop label1;
close stud_cur;
end//

the proc-creation query gets executed. But when I call stugrade() gives me an error 1241 (21000).


Solution

  • It looks to me like this line is causing your problem.

    set total=(v_sub1,v_sub2,v_sub3);
    

    MySQL can only handle scalars in this sort of context. You may want

    set total = v_sub1+v_sub2+v_sub3;
    

    Also, CONCAT() will take multiple parameters. That is, while this is correct:

    select concat(concat(concat('marks-->',v_sub1),v_sub2),v_sub3) as "subject wise marks";
    

    It can be recast as

    select concat('marks-->',v_sub1,v_sub2,v_sub3) as "subject wise marks";
    

    if it's easier.