光标不能在存储过程中工作,它会抛出1064错误

时间:2014-01-07 07:19:02

标签: mysql

我想在我的项目中使用游标,但它会抛出错误1064。请帮我解决问题.....

  

错误代码:1064您的SQL语法出错;查看与您的MySQL服务器版本对应的手册,以便在第63行“DECLARE curs1 CURSOR FOR SELECT healthinsurancepremium.productid,healthinsuran”附近使用正确的语法

我的程序代码:

CR-EATE DEFINER=`root`@`localhost` PROCEDURE `spGettestOffline` (
   IN in_sumassured INT(10),
   IN in_age INT(3),
   IN in_adult INT(4),
   IN in_child INT(4),
   IN in_tenure INT(3),
   IN in_city VARCHAR(20)
)

BEGIN

    DECLARE bDone INT DEFAULT 0;
    DECLARE var1 INT ;  
    DECLARE Var2 INT;
    DECLARE Var3 INT;
    DECLARE var4 INT;

    /* this is the table declaration */  
    DROP TEMPORARY TABLE IF EXISTS tblResults;
        CREATE TEMPORARY TABLE IF NOT EXISTS tblResults (
        productid INT,
        suminsured INT,
        amount INT,
        tenent INT
        );

    /* this is the cursor declaration */  
    DECLARE curs1 CURSOR FOR SELECT healthinsurancepremium.productid ,healthinsurancepremium.suminsured ,healthinsurancepremium.amount ,healthinsurancepremium.tenure FROM healthinsurancepremium LEFT JOIN `cityspecifichealthpremium` ON `healthinsurancepremium`.`id` >= `cityspecifichealthpremium`.`healthpremiumidmin` AND `healthinsurancepremium`.`id` <= `cityspecifichealthpremium`.`healthpremiumidmax` WHERE (`cityspecifichealthpremium`.`healthpremiumidmax` IS NULL AND `cityspecifichealthpremium`.`healthpremiumidmin` IS NULL) AND healthinsurancepremium.suminsured = in_sumassured AND in_age >=healthinsurancepremium.minage AND in_age <=healthinsurancepremium.maxage AND healthinsurancepremium.adult=in_adult AND healthinsurancepremium.child=in_child ;

    /* this is the cursor looping */  
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET bDone = 1;
    OPEN curs1;

    read_loop: LOOP
       FETCH curs1 INTO var1,var2,var3,var4;

       INSERT INTO tblResults VALUES (var1,var2, var3,var4);

       IF (bDone = 1) THEN 
           LEAVE read_loop;
       END IF;

    END LOOP;
    CLOSE curs1;

    SELECT * FROM tblResults;

END$$

我认为这个问题属于游标声明部分....但我没有找到任何内容

1 个答案:

答案 0 :(得分:1)

您已将DECLARE声明放在错误的位置。

DECLARE语句的使用条件:

  

您必须在BEGIN / END块的开头明确DECLARE及其数据类型。

将所有DECLARE ...语句移至BEGIN块的开头。 它应该看起来像:

BEGIN
    DECLARE bDone INT DEFAULT 0;
    DECLARE var1 INT ;  
    DECLARE Var2 INT;
    DECLARE Var3 INT;
    DECLARE var4 INT;
    DECLARE curs1 CURSOR FOR
        SELECT
          healthinsurancepremium.productid, 
          healthinsurancepremium.suminsured,
          healthinsurancepremium.amount,
          healthinsurancepremium.tenure
        FROM healthinsurancepremium
          LEFT JOIN `cityspecifichealthpremium` ON 
            `healthinsurancepremium`.`id` >= `cityspecifichealthpremium`.`healthpremiumidmin` AND
            `healthinsurancepremium`.`id` <= `cityspecifichealthpremium`.`healthpremiumidmax`
          WHERE 
           ( `cityspecifichealthpremium`.`healthpremiumidmax` IS NULL AND
             `cityspecifichealthpremium`.`healthpremiumidmin` IS NULL ) AND
           healthinsurancepremium.suminsured = in_sumassured AND 
           in_age >= healthinsurancepremium.minage AND 
           in_age <= healthinsurancepremium.maxage AND 
           healthinsurancepremium.adult = in_adult AND 
           healthinsurancepremium.child = in_child ;

请参阅 Stored Procedures - MySQL