2017-03-02 83 views
2

我有這樣的過程:錯誤1064在MySQL存儲過程

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 

和它工作正常我的機器上,但是當我嘗試在服務器上運行它,它返回:

ERROR 1064:你的SQL語法有錯誤;檢查手冊 對應於您的MySQL服務器版本的正確語法爲 使用'offsetcount; DECLARE NOT QUANDLER FOR NOT FOUND SET v_finished = 1; OP'31行

我正在使用Workbench 5.5.54,而我們的服務器正在運行5.1.66。我一直在尋找一個小時的答案,但我沒有運氣。希望有人幫助。謝謝!

回答

1

嘗試刪除這個:

offset @offsetcount; 

這是你錯誤的原因

嘗試添加此:

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

然後在使用查詢的WHERE子句 「where subid > @offsetcount

我想你需要使用while loop只是爲了檢查表我f有記錄或完成所有記錄。

+0

我真的需要這個偏移量,因爲我運行的記錄過多導致超時,所以我需要按批處理。 –

+0

用變量偏移不起作用,而不是將其更改爲像10這樣的實數,或者任何數字都不是變量。 –

+0

但我需要它來增加,以便它遍歷我的所有數據。我的電腦可以運行它,但我們的服務器不能。我的電腦不是那麼高,所以我需要我們的服務器來完成這項工作。 –