创建触发器时mysql中出现#1413错误

use*_*251 2 mysql database triggers

在phpmyadmin中使用mysql我开始创建一个触发器..但是我收到了这个错误

MySQL said: #1413 - Duplicate handler declared in the same block
Run Code Online (Sandbox Code Playgroud)

我不知道出了什么问题.我也是第一次用三个循环创建触发器..我也是初学者..帮我解决这个问题......这可能是一个重复的问题.我需要了解我的触发器有什么问题.插入到表后将执行此触发器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
Run Code Online (Sandbox Code Playgroud)

这可能是一个太多的代码.但我需要一个明确的答案..等待答案..

Rav*_*ddy 5

错误消息非常清楚,Duplicate处理程序在同一个块中声明.

当您有多个游标时,您可以not found适当地使用单个处理程序.

备选方案1可能性:

  1. 循环通过Cursor 1不应该调用Cursor 2和Cursor 3中的任何一个的打开.
  2. 然后,在关闭Cursor 1时,重置相同的done变量并使用下一个光标.
  3. 打开下一个光标并按照上述两个步骤操作
  4. 使用此选项可能,您不需要在同一个块中声明多个 相同类型的处理程序.

备选方案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;
Run Code Online (Sandbox Code Playgroud)

文件参考:

DECLARE语法

DECLARE只允许在BEGIN ... END复合语句中使用,并且在任何其他语句之前必须在其开头.

声明必须遵循一定的顺序.游标声明必须出现在处理程序声明之前.变量和条件声明必须出现在游标或处理程序声明之前