在MySQL中使用带PREPARE的LIMIT变量时出现未声明的变量错误

时间:2012-04-08 17:10:03

标签: mysql sql limit prepared-statement

我正在编写存储过程。我在使用变量和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; 
//

2 个答案:

答案 0 :(得分:1)

您可以使用INSERT ... SELECT执行相同的操作 -

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

答案 1 :(得分:0)

你的意思是?

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