我希望动态sql在mysql中创建一个存储过程。例如:
delimiter ///
create procedure x1()
begin
set @sql = '
drop procedure if exists x2;
delimiter ///
create procedure x2()
begin
INSERT INTO `world`.`city`
(`Name`,
`CountryCode`,
`District`,
`Population`)
VALUES
(''Meyerton'',
''ZAR'',
''Africa'',
500);
end ///
delimiter ;
call x2();
drop procedure if exists x2;';
prepare stmt1 from @sql;
execute stmt1;
deallocate prepare stmt1;
end ///
delimiter ;
call x1();
但是当我调用过程x1
时,我收到以下错误Error Code: 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 'DELIMITER /// CREATE PROCEDURE 20140416_TriggerSP_NoUserFound() BEGIN DECLARE ' at line 3
这是sql代码的实际触发器。上面的sql代码是实际sql代码的简化版本。
所以问题是,存储过程中的动态sql可以在mysql中创建存储过程吗? 如果需要,请加载原始SQL。
答案 0 :(得分:1)
来自MySQL 5.5 manual:
以下SQL语句可用作预准备语句:
ALTER TABLE
ANALYZE TABLE
CACHE INDEX
CALL
CHANGE MASTER
CHECKSUM {TABLE | TABLES}
COMMIT
{CREATE | RENAME | DROP} DATABASE
{CREATE | DROP} INDEX
{CREATE | RENAME | DROP} TABLE
{CREATE | RENAME | DROP} USER
{CREATE | DROP} VIEW
DELETE
DO
FLUSH {TABLE | TABLES | TABLES WITH READ LOCK | HOSTS | PRIVILEGES
| LOGS | STATUS | MASTER | SLAVE | DES_KEY_FILE | USER_RESOURCES}
GRANT
INSERT
INSTALL PLUGIN
KILL
LOAD INDEX INTO CACHE
OPTIMIZE TABLE
REPAIR TABLE
REPLACE
RESET {MASTER | SLAVE | QUERY CACHE}
REVOKE
SELECT
SET
SHOW {AUTHORS | CONTRIBUTORS | WARNINGS | ERRORS}
SHOW BINLOG EVENTS
SHOW CREATE {PROCEDURE | FUNCTION | EVENT | TABLE | VIEW}
SHOW {MASTER | BINARY} LOGS
SHOW {MASTER | SLAVE} STATUS
SLAVE {START | STOP}
TRUNCATE TABLE
UNINSTALL PLUGIN
UPDATE
MySQL 5.5中不支持其他语句。
因此无法创建程序。
答案 1 :(得分:0)
我找到了答案。
您无法在mysql中使用动态SQL创建存储过程。
如果您使用sql文本并在mysql workbench中运行它,您将看到无论您使用动态SQL创建的存储过程,它都将失败。