2016-04-24 50 views
0
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 ; 

,我得到这个错误找不到哪里是MySQL的语法错误

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 
+1

OMG男孩,请学习如何正确设置问题的格式。下次在SO编辑器中使用'{}'按钮。 – PerlDuck

+0

有一点谷歌搜索给了我[这个答案](http://stackoverflow.com/a/11000907/5830574)。也许它适合你的需求?我认为'@'符号是非参数的关键。 – PerlDuck

+0

@PerlDog对不起,这个错误。我修好了它。请帮助 – RGB

回答

0

是否表SMSTemplate有场F_ID

如果没有,那可能是你为什么得到这个错误。

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 ; 
+0

此外,如果你是新来的MySQL学习集和关系,并完全避免游标。游标主要由不懂sql的人员使用,并且想要考虑程序。他们表现得像狗一样。 – Drew

+0

谢谢,但我需要getFileDetails中的值来与第一个游标一起使用:) – RGB