mysqlstored-proceduressql-insertmysql-select-db

Need to Create MySQL Procedure for getting data from table based on condition and insert them into another table


Using a single MySQL procedure i need to write queries to get data from a table (where the results can be of a list also) after getting the results,need to insert those selected rows into another table of the same database..I'm finding dificulty in getting the result of the select query and fetching values to insert into another table...

Iam able to do the above one if it returns only one row but in my case it can return any number of rows...

DELIMITER $$ 

USE `scmn_nov21`$$

DROP PROCEDURE IF EXISTS `procedure1`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `procedure1`(
  IN Param1 VARCHAR(255),
  OUT Param2 VARCHAR(255),
  OUT Param3 VARCHAR(255)
)
BEGIN
  DECLARE myvar TEXT;

  SET myvar = (SELECT column1 FROM table1 WHERE column1 =2);

  INSERT INTO table1 (column1,column2,column3)
  VALUES (myvar,'Malaysia','Asia');

  COMMIT;
END$$

DELIMITER ;

Solution

  • I believe that you can do a Create as select: http://dev.mysql.com/doc/refman/5.7/en/create-table-select.html

    Returning malassia and asia as fixed values for your query...