是否可以在存储过程中使用动态生成的SQL在MySQL中创建触发器?我正在通过准备语句在我的过程中执行其他动态构造的查询,但是当我尝试相同的方法来创建触发器时,我得到以下错误:
ERROR Code: 1295This command is not supported in the prepared statement protocol yet
这个问题有解决方法吗?是否有另一种使用动态SQL创建触发器的方法?
基本上我要做的是动态创建触发器,用于记录各种不同表上插入的审计数据.我在* audit_tables *表中列出了我想要审核的表.下面的精简过程迭代该表中的条目并尝试创建触发器.
drop procedure if exists curtest;
delimiter |
create procedure curtest()
BEGIN
DECLARE done INT DEFAULT 0;
declare tn varchar(16);
declare cur cursor for select table_name from audit_tables;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
read_loop: LOOP
fetch cur into tn;
if done then
leave read_loop;
end if;
/* Create the BEFORE INSERT trigger */
set @sql = concat('CREATE TRIGGER audit_', tn, '_bi BEFORE INSERT ON ', tn, '
FOR EACH ROW BEGIN
set new.foo="bar";
END;');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
end LOOP;
close cur;
END;
|
delimiter ;
call curtest();