无法找到MySQL语法错误的位置

时间:2016-04-24 13:17:03

标签: mysql macos stored-procedures terminal procedure

delimiter %%
create procedure getFileID(in fname varchar(100), out fId int)
begin
    select ID from File
    where Name = fname
    into fId;
end %%
delimiter ;

delimiter $$
create procedure FileINFO(in fname varchar(100))
begin
    declare SMS_done, Par_don boolean default false;
    declare SMSID int;
    declare SMSName varchar(100) default "";
    declare SMSCode varchar(100) default "";
    declare ParamName varchar(100) default "";
    declare fId int default 0;
    call getFileID(fname, fId);
    declare c1 cursor for select ID, Code, Name from SMSTemplate where F_ID = fId;
    declare continue handler for not found set SMS_done = true;
    open c1;
    SMS_loop : loop
        fetch from c1 into SMSID, SMSCode, SMSName;
            if SMS_done then
                close c1;
                leave SMS_loop;
            end if;
        block2 : begin
        declare c2 cursor for 
            select Name from ParameterType where ST_ID = SMSID;
        declare continue handler for not found set Par_done = true;
        open c2;
        Par_loop : loop
            fetch from c2 into ParamName;
            if SMS_done then
            set SMS_done = false;
            close c2;
            leave Par_loop;
            end if;
        insert into FileDetails
        (FileName, SMSName, SMSCode, ParamName)
        values
        (fname, SMSName, SMSCode, ParamName);
        end loop Par_loop;
        end block2;
    end loop SMS_loop;
    select * from FileDetails;
end $$
delimiter ;

我得到了那个错误

ERROR 1064 (42000): 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 c1 cursor for select ID, Code, Name from SMSTemplate where F_ID = fId;' at line 10

2 个答案:

答案 0 :(得分:1)

对于初学者来说,声明都在顶部组合在一起。 也没有fetch into之类的东西,所以这两件事情都得到了解决。

Par_don / Par_done在声明中有拼写错误。

另外,为了理智,如果存储过程存在于每个存储过程之上,则会有一个丢弃,因此很容易编写和维护。

drop procedure if exists FileINFO;
delimiter $$
create procedure FileINFO(in fname varchar(100))
begin
    declare SMS_done, Par_done boolean default false;
    declare SMSID int;
    declare SMSName varchar(100) default "";
    declare SMSCode varchar(100) default "";
    declare ParamName varchar(100) default "";
    declare fId int;
    declare c1 cursor for select ID, Code, Name from SMSTemplate where F_ID = fId;
    declare continue handler for not found set SMS_done = true;
    call getFileID(fname,  fId );



    open c1;

    SMS_loop : loop
        fetch c1 into SMSID, SMSCode, SMSName;
            if SMS_done then
                close c1;
                leave SMS_loop;
            end if;
        block2 : begin
        declare c2 cursor for 
            select Name from ParameterType where ST_ID = SMSID;
        declare continue handler for not found set Par_done = true;
        open c2;
        Par_loop : loop
            fetch from c2 into ParamName;
            if SMS_done then
            set SMS_done = false;
            close c2;
            leave Par_loop;
            end if;
        insert into FileDetails
        (FileName, SMSName, SMSCode, ParamName)
        values
        (fname, SMSName, SMSCode, ParamName);
        end loop Par_loop;
        end block2;
    end loop SMS_loop;
    select * from FileDetails;
end $$
delimiter ;

答案 1 :(得分:0)

SMSTemplate是否有字段F_ID ??

如果没有,那可能是您收到此错误的原因。