创建用户和授予权限的sql过程中出现语法错误

olmpazwi  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(437)

有人能指出我为什么会有语法错误吗?每当我尝试将其输入控制台时,我都会得到“expre2”的语法错误,但我没有得到,因为它看起来很好:

DELIMITER $$

DROP PROCEDURE IF EXISTS `add_User`$$

CREATE PROCEDURE `add_User`(IN `p_Name` VARCHAR(45), IN `p_Passw` VARCHAR(200), IN NIF INT(12))
BEGIN
    DECLARE `_HOST` CHAR(14) DEFAULT '@\'localhost\'';
    SET `p_Name` := CONCAT('\'', REPLACE(TRIM(`p_Name`), CHAR(39), CONCAT(CHAR(92), CHAR(39))), '\''),
    `p_Passw` := CONCAT('\'', REPLACE(`p_Passw`, CHAR(39), CONCAT(CHAR(92), CHAR(39))), '\'');
    SET @`sql` := CONCAT('CREATE USER ', `p_Name`, `_HOST`, ' IDENTIFIED BY ', `p_Passw`);
    PREPARE `stmt` FROM @`sql`;
    EXECUTE `stmt`;
    DEALLOCATE PREPARE `stmt`;
    FLUSH PRIVILEGES;
    SET @`expre` := CONCAT('GRANT SELECT, INSERT, UPDATE ON trabalho.ver_assist TO ', `p_Name`);
PREPARE `stmt2` FROM @`expre`;
EXECUTE `stmt2`;
DEALLOCATE `stmt2`;
    SET @`expre2` := CONCAT('GRANT SELECT, INSERT, UPDATE ON trabalho.ver_saidas TO ', `p_Name`);
PREPARE `stmt3` FROM @`expre2`;
EXECUTE `stmt3`;
DEALLOCATE `stmt3`;
FLUSH PRIVILEGES;
END$$

我真的不明白为什么它不起作用。。。

ql3eal8s

ql3eal8s1#

你错过了一个 PREPAREDEALLOCATE stmt2 以及 DEALLOCATE stmt3 也。请在下面找到正确的查询:

DROP PROCEDURE IF EXISTS `add_User`$$

CREATE PROCEDURE `add_User`(IN `p_Name` VARCHAR(45), IN `p_Passw` VARCHAR(200), IN NIF INT(12))
BEGIN
    DECLARE `_HOST` CHAR(14) DEFAULT '@\'localhost\'';
    SET `p_Name` := CONCAT('\'', REPLACE(TRIM(`p_Name`), CHAR(39), CONCAT(CHAR(92), CHAR(39))), '\''),
    `p_Passw` := CONCAT('\'', REPLACE(`p_Passw`, CHAR(39), CONCAT(CHAR(92), CHAR(39))), '\'');
    SET @`sql` := CONCAT('CREATE USER ', `p_Name`, `_HOST`, ' IDENTIFIED BY ', `p_Passw`);
    PREPARE `stmt` FROM @`sql`;
    EXECUTE `stmt`;
    DEALLOCATE PREPARE `stmt`;
    FLUSH PRIVILEGES;
    SET @`expre` := CONCAT('GRANT SELECT, INSERT, UPDATE ON trabalho.ver_assist TO ', `p_Name`);
PREPARE `stmt2` FROM @`expre`;
EXECUTE `stmt2`;
DEALLOCATE PREPARE `stmt2`;
    SET @`expre2` := CONCAT('GRANT SELECT, INSERT, UPDATE ON trabalho.ver_saidas TO ', `p_Name`);
PREPARE `stmt3` FROM @`expre2`;
EXECUTE `stmt3`;
DEALLOCATE PREPARE `stmt3`;
FLUSH PRIVILEGES;
END$$
jm2pwxwz

jm2pwxwz2#

您的set语句不正确,您必须选择set@sql:=(select concat('create user', p_Name , _HOST ,'标识人', p_Passw ));
你的2个deallocate是不正确的语法是deallocate prepare something
而且里面有很多不必要的背勾。

相关问题