【发布时间】:2014-04-17 17:20:00
【问题描述】:
我希望动态 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。
【问题讨论】: