title: Loop the select query result set in MYSQL Stored Procedure
body: <p>How to loop the resultset of query and in loop the resultset get the column value and fire insert query on database.</p>
  
  <p>Below is My SP:</p>
  
  <pre><code>Input parameter listvalues and value is 1,2,3,4,5
  
  SET @t1 = CONCAT("SELECT ID FROM interest WHERE ID IN(",listvalues,")");
  PREPARE stmt1 FROM @t1;
  EXECUTE stmt1;
  DEALLOCATE PREPARE stmt1;
  </code></pre>
  
  <p>Then how to loop the stmt1 to get all ID values to insert in another table.</p>
  
  <p><strong>My SP:</strong></p>
  
  <pre><code>BEGIN 
  INSERT INTO `registration`(`FirstName`,`LastName`,`EMail`,`PhoneNumber`,`Gender`,`State`,`City`,`ImagePath`,`IsDeleted`,`CreatedDate`,`ModifiedDate`)
  VALUES(FirstName,LastName,EMail,PhoneNumber,Gender,State,City,ImagePath,0,NOW(),NOW());
  
  SET @RegID = LAST_INSERT_ID();
  
  SET @t1 = CONCAT("INSERT INTO `userinterest` (`InterestId` , `UserId`) VALUES((SELECT ID FROM interest WHERE ID IN(",InterestList,")),",@RegID,")");
  PREPARE stmt3 FROM @t1;
  EXECUTE stmt3;
  DEALLOCATE PREPARE stmt3;
  
  END
  </code></pre>
  
  <p>But it give error <strong>Subquery returns more than 1 row</strong></p>