存储过程在MySql中无法正常工作?

时间:2013-06-03 09:08:09

标签: mysql sql stored-procedures

我一直在调试一个SQL存储过程,它必须根据表C中的值(ID)取值(在我的代码ID和Numb中)表A,然后对Numb求平方并将其存储在表B中即所有的东西ID,Numb和Square。 我无法在下面的代码中找出问题

 DELIMITER $$
    CREATE PROCEDURE matlab.squaring
     BEGIN
       DECLARE finish BOOLEAN DEFAULT 0; # <- set up initial conditions
       DECLARE square BIGINT(10);
       DECLARE ID INT(10);
       DECLARE Numb INT (10);
       DECLARE id_cur CURSOR FOR
         SELECT ID, Numb FROM A WHERE EXISTS ( SELECT ID FROM c);
         SET @square= @Numb * @Numb

           INSERT INTO B
           (
             ID ,
             Numb ,
             square
           )     values ( ID , Numb, square);


       DECLARE CONTINUE HANDLER FOR NOT FOUND SET finish = TRUE;
       OPEN id_cur;

     the_loop : LOOP
        FETCH id_cur INTO ID;

        IF finish THEN
           CLOSE id_cur;
           LEAVE the_loop;
        END IF
    END LOOP the_loop;
    END$$

当我运行存储过程时,弹出的错误是“您的代码中似乎存在一些语法错误,请参阅MYSql指南。”

编辑: 还有一个帮助请如何执行此存储过程。

3 个答案:

答案 0 :(得分:0)

  • 你在PROCEDURE matlab之后错过了()...
  • 并且;在END IF之后
  • 另外,HANDLER声明应该在任何可执行代码之前 在CURSOR声明之后
  • SET @ square = @Numb之后的分号*需要@Numb

所以,查询应该是这样的:

    DELIMITER $$
CREATE PROCEDURE matlab.squaring ()
 BEGIN
   DECLARE finish BOOLEAN DEFAULT 0; # <- set up initial conditions
   DECLARE square BIGINT(10);
   DECLARE ID INT(10);
   DECLARE Numb INT (10);
   DECLARE id_cur CURSOR FOR
     SELECT ID, Numb FROM A WHERE EXISTS ( SELECT ID FROM c);
   DECLARE CONTINUE HANDLER FOR NOT FOUND SET finish = TRUE;
     SET @square= @Numb * @Numb;

       INSERT INTO B
       (
         ID ,
         Numb ,
         square
       )     values ( ID , Numb, square);

   OPEN id_cur;

 the_loop : LOOP
    FETCH id_cur INTO ID;

    IF finish THEN
       CLOSE id_cur;
       LEAVE the_loop;
    END IF;
END LOOP the_loop;
END$$

答案 1 :(得分:0)

在endif处错过了参数括号和分号。

DELIMITER $$
    CREATE PROCEDURE squaring()
     BEGIN
       DECLARE finish BOOLEAN DEFAULT 0; # <- set up initial conditions
       DECLARE square BIGINT(10);
       DECLARE ID INT(10);
       DECLARE Numb INT (10);
       DECLARE id_cur CURSOR FOR
       SELECT ID, Numb FROM A WHERE EXISTS ( SELECT ID FROM c);
       DECLARE CONTINUE HANDLER FOR NOT FOUND SET finish = TRUE;
       SET @square= @Numb * @Numb;

           INSERT INTO B
           (
             ID ,
             Numb ,
             square
           )     values ( ID , Numb, square);


       OPEN id_cur;

     the_loop : LOOP
        FETCH id_cur INTO ID;

        IF finish THEN
           CLOSE id_cur;
           LEAVE the_loop;
        END IF;
    END LOOP the_loop;
    END$$

答案 2 :(得分:0)

有各种小错误;

您需要一个参数列表,即使该过程为空;

CREATE PROCEDURE matlab.squaring()

continue处理程序需要在其他声明之下;

   DECLARE id_cur CURSOR FOR
     SELECT ID, Numb FROM A WHERE EXISTS ( SELECT ID FROM c);
   DECLARE CONTINUE HANDLER FOR NOT FOUND SET @finish = TRUE;

你忘记了分号;

SET @square= @Numb * @Numb;

你在变量用法上忘了@;

   )     values ( @ID , @Numb, @square);

你忘记了END IF上的分号

    END IF;

就像概述一样,这里有更新完整的内容;

CREATE PROCEDURE matlab.squaring()
 BEGIN
   DECLARE finish BOOLEAN DEFAULT 0; # <- set up initial conditions
   DECLARE square BIGINT(10);
   DECLARE ID INT(10);
   DECLARE Numb INT (10);
   DECLARE id_cur CURSOR FOR
     SELECT ID, Numb FROM A WHERE EXISTS ( SELECT ID FROM c);
   DECLARE CONTINUE HANDLER FOR NOT FOUND SET @finish = TRUE;
   SET @square= @Numb * @Numb;

   INSERT INTO B
   (
     ID ,
     Numb ,
     square
   )     values ( @ID , @Numb, @square);

 OPEN id_cur;

 the_loop : LOOP
    FETCH id_cur INTO ID;

    IF finish THEN
       CLOSE id_cur;
       LEAVE the_loop;
    END IF;
END LOOP the_loop;
END//