title: Undeclared variable error when using a LIMIT variable with PREPARE in MySQL
body: <p>I'm writing a stored procedure. I am having some problems using a variable in combination with LIMIT and a prepared statement. (See code below)</p>
  
  <p>The procedure is created normally, but when I call it I get an error that variable <code>a</code> is undeclared. Any suggestions? </p>
  
  <pre><code>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&lt; 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&lt; row_numbers+1 DO
          SET counter=counter+1;
      END WHILE;
  end; 
  //
  </code></pre>