mySQL值语句作为参数?

发布于 2025-01-30 16:43:15 字数 1172 浏览 3 评论 0原文

是否可以将代码的整个值块作为参数插入MySQL表中?

参数将是:

  ("(638) 833-5496","[email protected]","Spain"),
  ("(293) 742-0229","[email protected]","Belgium"),
  ("1-265-156-4304","[email protected]","Ireland"),
  ("1-833-780-2553","[email protected]","France"),
  ("(619) 691-0656","[email protected]","Costa Rica");

插入语句是否

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

可以作为插入语句,存储过程或其他任何内容进行?

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","[email protected]","Spain"),
  ("(293) 742-0229","[email protected]","Belgium"),
  ("1-265-156-4304","[email protected]","Ireland"),
  ("1-833-780-2553","[email protected]","France"),
  ("(619) 691-0656","[email protected]","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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

佼人 2025-02-06 16:43:15

我能够通过使用execute命令并将字符串作为变量,然后将其编译到一个SQL语句中来解决此问题。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文