在phpmyadmin中使用mysql我开始创建一个触发器.. 但我得到了这个错误
MySQL said: #1413 - Duplicate handler declared in the same block
我不知道出了什么问题..我也是第一次用三个循环创建触发器..我也是初学者..帮我解决这个问题..
这可能是一个重复的问题。我需要了解我的触发器有什么问题。
插入表external
我的触发器包含
BEGIN
DECLARE scode varchar(30);
DECLARE grade varchar(30);
DECLARE val integer;
DECLARE credit integer;
DECLARE gval integer;
DECLARE temp double;
DECLARE cgpa decimal(4,3);
DECLARE t double;
DECLARE done int default false;
DECLARE done1 int default false;
DECLARE done2 int default false;
DECLARE cur1 CURSOR FOR SELECT Sub_Code,Grade FROM external where Reg_No=new.Reg_No;
DECLARE continue handler for not found set done=true;
DECLARE continue handler for not found set done1=true;
DECLARE continue handler for not found set done2=true;
SET credit=0;
OPEN cur1;
my_loop: loop
set done=false;
fetch cur1 into scode,grade;
if done then
leave my_loop;
end if;
DECLARE cur2 CURSOR FOR SELECT Credits FROM subj_mast WHERE Sub_Code=scode;
OPEN cur2;
my_loop1: loop
set done1=false;
fetch cur2 into val;
if done1 then
leave my_loop1;
end if;
set credit=credit+val;
DECLARE cur3 CURSOR FOR SELECT gvalue FROM gradevalue where Grade=grade;
OPEN cur3;
my_loop2: loop
set done2=false;
fetch cur3 into gval;
if done2 then
leave my_loop2;
end if;
set temp=val*gval;
set t=t+temp;
end loop my_loop;
end loop my_loop1;
end loop my_loop2;
set cgpa=t/credit;
close cur1;
close cur2;
close cur3;
insert into result values(new.Reg_No,cgpa);
END
这可能是一个太多的代码。但我需要一个明确的答案..等待答案..
答案 0 :(得分:5)
错误消息非常明确在同一块中声明的重复处理程序。
如果您有多个游标,则可以谨慎使用单个not found
处理程序。
选项1可能性:
done
变量并使用下一个光标。选项2可能性:
在您的代码中,您在循环浏览以前的游标获取结果时声明其他游标。那部分是另一个错误。您无论何时何地都无法 DECLARE
。所有 DECLARE
语句必须位于 BEGIN
块之上。因此,如果要根据另一个游标的获取结果定义另一个游标,请使用另一个 BEGIN --- END
块。
按如下方式更改触发器主体:
BEGIN
DECLARE scode varchar(30);
DECLARE grade varchar(30);
DECLARE val integer;
DECLARE credit integer;
DECLARE gval integer;
DECLARE temp double;
DECLARE cgpa decimal(4,3);
DECLARE t double;
DECLARE done int default false;
DECLARE cur1 CURSOR FOR
SELECT Sub_Code, Grade FROM external where Reg_No = new.Reg_No;
DECLARE continue handler for not found set done = true;
SET credit = 0;
OPEN cur1;
my_loop: loop
fetch cur1 into scode, grade;
if done then
leave my_loop;
end if;
BEGIN
DECLARE cur2 CURSOR FOR
SELECT Credits FROM subj_mast WHERE Sub_Code = scode;
DECLARE continue handler for not found set done1 = true;
OPEN cur2;
my_loop1: loop
fetch cur2 into val;
if done1 then
leave my_loop1;
end if;
set credit = credit + val;
BEGIN
DECLARE cur3 CURSOR FOR
SELECT gvalue FROM gradevalue where Grade = grade;
DECLARE done2 int default false;
OPEN cur3;
my_loop2: loop
fetch cur3 into gval;
if done2 then
leave my_loop2;
end if;
set temp = val * gval;
set t = t + temp;
end loop my_loop2;
close cur3;
END;
end loop my_loop1;
close cur2;
END;
end loop my_loop;
close cur1;
set cgpa = t / credit;
insert into result values( new.Reg_No, cgpa );
END;
文档参考:
仅在DECLARE
复合语句中允许
BEGIN ... END
并且必须在任何其他陈述之前开始。声明必须遵循一定的顺序。游标声明必须 出现在处理程序声明之前。变量和条件 声明必须出现在游标或处理程序声明之前