gpt4 book ai didi

MySQL CREATE USER 带有变量?

转载 作者:IT老高 更新时间:2023-10-29 00:21:28 24 4
gpt4 key购买 nike

我想这样做:

#Variables
SET @username="jdoe", @password="secret";

# Insert a new MySQL User
CREATE USER @username@'localhost' IDENTIFIED BY @password;GRANT USAGE ON *.* TO @username@'localhost' IDENTIFIED BY @password WITH MAX_QUERIES_PER_HOUR 120 MAX_CONNECTIONS_PER_HOUR 60 MAX_UPDATES_PER_HOUR 60 MAX_USER_CONNECTIONS 2;

但是得到:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@username@'localhost' IDENTIFIED BY @password' at line 2

如何在 CREATE USER 语句中使用变量?

最佳答案

你可以使用动态SQL作为:

SET @query1 = CONCAT('
CREATE USER "',@username,'"@"localhost" IDENTIFIED BY "',@password,'" '
);
PREPARE stmt FROM @query1; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @query1 = CONCAT('
GRANT USAGE ON *.* TO "',@username,'"@"localhost" IDENTIFIED BY "',@password,'" WITH
MAX_QUERIES_PER_HOUR 120 MAX_CONNECTIONS_PER_HOUR 60 MAX_UPDATES_PER_HOUR 60
MAX_USER_CONNECTIONS 2'
);
PREPARE stmt FROM @query1; EXECUTE stmt; DEALLOCATE PREPARE stmt;

关于MySQL CREATE USER 带有变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11951570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com