简体   繁体   English

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

[英]Undeclared variable error when using a LIMIT variable with PREPARE in MySQL

I'm writing a stored procedure.我正在写一个存储过程。 I am having some problems using a variable in combination with LIMIT and a prepared statement.我在将变量与 LIMIT 和准备好的语句结合使用时遇到了一些问题。 (See code below) (见下面的代码)

The procedure is created normally, but when I call it I get an error that variable a is undeclared.该过程是正常创建的,但是当我调用它时出现一个错误,指出变量a未声明。 Any suggestions?有什么建议么?

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; 
//

You could use an INSERT... SELECT to do the same thing -您可以使用 INSERT... SELECT 来做同样的事情 -

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

Did you mean?你的意思?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM