Mysql存储过程出错1064

时间:2017-03-02 09:25:39

标签: mysql stored-procedures

我有这个程序:

 CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_billing_details`()
BEGIN

 DECLARE offsetcount INTEGER DEFAULT 0;
 DECLARE totalcount INTEGER DEFAULT 0;

 set totalcount = (select count(*) from new.billing);


 loopall: LOOP


 BEGIN

 DECLARE v_finished INTEGER DEFAULT 0;
 DECLARE c_subsid varchar(100) DEFAULT "";
 DECLARE c_duedate varchar(100) DEFAULT "";
 DECLARE c_descriptions varchar(100) DEFAULT "";
 DECLARE c_debitamt varchar(100) DEFAULT "";
 DECLARE c_transdate varchar(100) DEFAULT "";

 DECLARE c_cursor CURSOR FOR 
 SELECT subsid, descriptions, debitamt, transdate FROM new.billing limit 200 offset offsetcount;

 -- declare NOT FOUND handler
 DECLARE CONTINUE HANDLER 
        FOR NOT FOUND SET v_finished = 1;

 OPEN c_cursor;

 loopbilling: LOOP

 FETCH c_cursor INTO c_subsid, c_descriptions, c_debitamt, c_transdate;

 IF v_finished = 1 THEN 
 LEAVE loopbilling;
 END IF;


insert into sakura.customer_bill_items (type ,name, description, amount, created_at) values (if(c_descriptions = "balance", 4, if(c_descriptions = "subscription", 1 , if(( c_descriptions like "%modem%" or c_descriptions like "%amplifier%" or c_descriptions like "%power supply%"), 2 , 3))) ,c_descriptions, c_descriptions, c_debitamt, c_transdate);

insert into sakura.customer_monthly_bill_items (customer_id, bill_item_id, start_billing_date) values ((select a.id from sakura.customer a where a.subscriber_id = c_subsid), last_insert_id(), c_transdate); 

insert into sakura.customer_billing_details (bill_item_id, subscription_plan_id, bill_item_amount) values (last_insert_id(), (select subscription_plan_id from sakura.customer_subscriptions where customer_id = (select id from sakura.customer where subscriber_id = (select subsid from new.subscriber where subsid = c_subsid))), c_debitamt);

insert into sakura.customer_billing_header (customer_id) select a.id from sakura.customer a where a.subscriber_id = c_subsid;

update sakura.customer_billing_details set header_id = last_insert_id() where id = last_insert_id();

 END LOOP loopbilling;

 CLOSE c_cursor;

END;


 set offsetcount = offsetcount + 200;

IF offsetcount >= totalcount THEN 
 LEAVE loopall;
 END IF;


 end LOOP loopall;

END

并且它在我的机器上正常工作但是当我尝试在服务器上运行它时,它返回:

  

错误1064:您的SQL语法出错;检查手册   对应于您的MySQL服务器版本,以获得正确的语法   使用near' offsetcount; DECLARE CONTINUE HANDLER for NOT FOUND SET   v_finished = 1; OP'在第31行

我正在使用Workbench 5.5.54,而我们的服务器正在运行5.1.66。我现在一直在寻找答案,但我没有运气。希望有人帮忙。谢谢!

1 个答案:

答案 0 :(得分:1)

尝试删除此内容:

offset @offsetcount;

这是导致错误的原因

尝试添加:

OPEN c_cursor;
     @offsetcount = @offsetcount + 1;
 loopbilling: LOOP

然后在where子句“where subid > @offsetcount

中使用查询

我认为你需要使用while loop来检查表是否有记录或完成所有记录。