1

我正在编写一个存储过程。我在将变量与 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; 
//
4

2 回答 2

1

您可以使用 INSERT ... SELECT 来做同样的事情 -

INSERT INTO warehouse.place (shop, fio)
SELECT 1, fio FROM salesmen
于 2012-04-08T19:12:51.730 回答
0

你的意思?

set @s='select fio from salesmen into a Limit ?,1';
于 2012-04-08T17:21:21.170 回答