2012-04-08 100 views
1

我在写一个存储过程。我在使用LIMIT和准备好的语句的变量时遇到了一些问题。 (见下面的代码)在MySQL中使用PREPARE使用LIMIT变量时未声明的变量错误

该过程正常创建,但当我打电话时,我得到一个错误,变量a未申报。有什么建议么?

delimiter // 
drop procedure if exists shop1; 
create procedure shop1() 
begin 
    declare a varchar(255) default null; 
    declare counter int(4) default 0; 
    declare row_numbers int(4); 
    declare s varchar(255) default null; 

    select count(*) into row_numbers from salesmen; 
    WHILE counter< row_numbers+1 DO 
     set @s='select fio from salesmen into a Limit ? 1'; 
     set @counter=counter; 
     prepare stmt from @s; 
     execute stmt using @counter; 

     SET counter=counter+1; 
     insert into warehouse.place (shop, fio) values (1, a); 
    END WHILE; 

    SET counter=0; 
    select count(*) into row_numbers from products; 
    WHILE counter< row_numbers+1 DO 
     SET counter=counter+1; 
    END WHILE; 
end; 
// 
+0

'' 没有帮助。错误是一样的。 – 2012-04-08 17:40:12

+0

我认为,即使没有必要。 – 2012-04-08 17:44:17

+0

不需要在循环中运行多个查询。改为使用游标 - [RefMan](http://dev.mysql.com/doc/refman/5.0/en/cursors.html) – nnichols 2012-04-08 19:07:17

回答

0

您的意思是?

set @s='select fio from salesmen into a Limit ?,1'; 
1

你可以使用一个INSERT ... SELECT做同样的事情 -

INSERT INTO warehouse.place (shop, fio) 
SELECT 1, fio FROM salesmen 
+0

比'LIMIT'循环更高效 – Leigh 2012-04-08 19:59:43