2013-06-03 80 views
0

我一直在調試一個SQL存儲過程,它必須根據表C中存在的值(ID)取值(在我的代碼ID和Numb中)形式的表A,然後平方Numb並將其存儲在表B中所有的東西ID,麻木和廣場。 我無法弄清楚這個問題在下面的代碼存儲過程在MySql中不能正常工作?

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的指導。「

編輯: 一個更多的幫助請如何執行此存儲過程。

+2

不確定,但行SET'@ square = @Numb * @ Numb'沒有分號。可能是這個問題。 – ankurtr

回答

0

有各種小錯誤;

即使過程爲空,也需要參數列表;

CREATE PROCEDURE matlab.squaring() 

繼續處理程序需要在其他聲明的正下方;

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); 

你忘了A端分號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// 
0
  • 你MATLAB程序之後錯過了()...
  • ;及END之後,如果
  • 此外,處理程序聲明應該是之前的任何可執行代碼和遊標聲明之後
  • SET @平方= @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$$ 
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$$