'Error mysql syntax use 'declare pfee int default null;',check the manual that corresponds to your MySQL server version
delimiter //
create procedure payFees(in deviceid int, in fee int)
begin
declare pbalance int default null;
declare ptype varchar(2) default "";
declare total int;
select balance, type into pbalance, ptype from device d where d.deviceid = deviceid;
if pbalance is null
then
select "cannot found the device" as result;
else
/* declare rate1 float;
declare rate2 float;
declare damage1 float;
declare damage2 float;
select rate1, rate2, damage1, damage2 into rate1, rate2, damage1, damage2 from charge_table ct where ct.type = ptype; */
set total = pbalance + fee;
update device d set balance = total where d.deviceid = deviceid;
declare pfee int default null;
declare pdate varchar(6) default null;
declare curl cursor for select basicfee, yearmonth from meter_record m, electricity_bill e where m.id = e.eid and m.deviceid = deviceid and flag = 0;
declare continue handler for not found set pfee = null;
open curl;
fetch curl into pfee, pdate;
while(pfee is not null) do
declare overyear bool;
if year(pdate) = year(now()) or (year(pdate) + 1 = year(now()) and month(pdate) = "12"):
then
set overyear = false;
else
set overyear = true;
end if;
select overyear;
fetch curl into payfee, pdate;
end while;
close curl;
end if;
end;
//
this is my sql code and I try to use cursor. but there is a mistake I have long troubled, I tried a lot of ways no solution.
the error message is: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'declare pfee int default null; declare pdate varchar(6) default null; declar' at line 20
please help!
Solution 1:[1]
You can't put your declare after other statements.
https://dev.mysql.com/doc/refman/5.7/en/declare.html says:
DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Bill Karwin |