我基本上尝试编写商店程序来创建用户。 sql如下:
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`john`@`%` PROCEDURE `create_user`(in userstr char(50), in pass char(50))
BEGIN
declare str char(52);
declare mypass char(52);
declare exec_str char(255);
set str = concat('\'', userstr, '\'@','\'%\'');
/*set str = concat('\'', userstr);*/
/*select str;*/
set mypass = concat('\'', pass, '\'');
set @t1 = concat('create user ', str, ' identified by ', mypass, '; GRANT ALL PRIVILEGES ON *.* TO ', str, ' WITH GRANT OPTION;');
select @t1;
prepare stmt1 from @t1;
/*select stmt1;*/
execute stmt1;
deallocate prepare stmt1;
END
上面的代码在执行之前会生成以下字符串。但是我收到了一个错误。
生成的SQL:
创建'splat'标识的用户'示例'@'%';授予所有特权*。*'示例'@'%'WITH GRANT OPTION;
然后它会崩溃,返回错误1064: 14:48:32调用create_user('example','splat')错误代码:1064你的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以便在上使用“GRANT ALL PRIVILEGES”附近使用正确的语法。 TO'示例'@'%'WITH GRANT OPTION'在第1行
我不知道发生了什么事。仅供参考,我甚至尝试在授予权限后转义*。
我正在运行的版本是:5.6.20
希望你能提供帮助,我相信这会有些愚蠢。但我似乎无法看到树木的木材。
亲切的问候 约翰
答案 0 :(得分:0)
你的主要问题是你有两个陈述,而不是一个。因此,您必须单独准备和执行它们:
set str = concat('\'', userstr, '\'@','\'%\'');
/*set str = concat('\'', userstr);*/
/*select str;*/
set mypass = concat('\'', pass, '\'');
set @t1 = concat('create user ', str, ' identified by ', mypass, ';');
select @t1;
prepare stmt1 from @t1;
/*select stmt1;*/
execute stmt1;
deallocate prepare stmt1;
set @t2 = CONCAT('GRANT ALL PRIVILEGES ON *.* TO ', str, ' WITH GRANT OPTION;');
prepare stmt2 from @t2;
execute stmt2;
deallocate prepare stmt2;