0

is it possible to insert into a MySQL table with the whole VALUES block of code being a parameter?

Parameter would be:

  ("(638) 833-5496","enim.curabitur@hotmail.couk","Spain"),
  ("(293) 742-0229","odio.semper@yahoo.net","Belgium"),
  ("1-265-156-4304","tincidunt.dui.augue@outlook.net","Ireland"),
  ("1-833-780-2553","scelerisque.scelerisque@aol.com","France"),
  ("(619) 691-0656","ac.risus.morbi@icloud.org","Costa Rica");

Insert statement would be

INSERT INTO `myTable` (`phone`,`email`,`country`)
VALUES
  {parameter}

Is it possible to do as an Insert statement, stored procedure, or anything?

Austin
  • 37
  • 7
  • Check if this solves your problem [link](https://stackoverflow.com/questions/5526917/how-to-do-a-batch-insert-in-mysql) – Amit Verma May 20 '22 at 02:52
  • Shown data block must be not a single parameter (which will be treated as solid string parameter and enclosed with single quotes) but a code block to be appended. – Akina May 20 '22 at 04:19

1 Answers1

0

I was able to solve this by utilizing the EXECUTE command and passing the string as a variable and then compiling it into one SQL statement.

CREATE DEFINER=`admin`@`%` PROCEDURE `insert_string_storedprocedure`(bubble_variable text)
BEGIN

set @insert_string = "INSERT INTO `myTable` 
(`phone`,`email`,`country`)
VALUES
(insert_statement)
AS new
ON DUPLICATE KEY UPDATE
email = new.email;";

set @fullcommand = REPLACE(@insert_string, '(insert_statement)', bubble_variable);

PREPARE stmt FROM @fullcommand;
EXECUTE stmt;

END
Austin
  • 37
  • 7