我有一个存储过程创建一个新的触发器,我不能通过PREPARE stmt
和EXECUTE
执行这个sql,我创建一个带触发器创建代码的外部sql文件。
我可以在存储过程中执行此脚本sql吗?
有一条指令在MySQL控制台中像SOURCE
一样工作吗?
这是我的存储过程:
BEGIN
DECLARE Finished BOOL DEFAULT FALSE;
DECLARE TableName VARCHAR(255);
DECLARE TablesCursor CURSOR FOR
SELECT
t1.`TABLE_NAME`
FROM
`INFORMATION_SCHEMA`.`TABLES` AS t1 LEFT JOIN `register` AS t2
ON(t1.`TABLE_NAME` = t2.`table_name`)
WHERE
t1.`TABLE_SCHEMA` = "sft_test"
AND
t1.`TABLE_NAME` != "register"
AND
t2.`table_name` IS NULL;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET Finished = TRUE;
OPEN TablesCursor;
MainLoop: LOOP
FETCH TablesCursor INTO TableName;
IF Finished THEN
LEAVE MainLoop;
END IF;
SET @strSql = CONCAT('INSERT INTO `register` (`table_name`,`record_number`,`creation_datetime`) SELECT "', TableName, '" AS "table_name", (SELECT COUNT(1) FROM `', TableName, '`) AS "record_number", NOW() AS "creation_datetime";');
PREPARE stmt FROM @strSql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @strSqlFilePath = CONCAT('"', REPLACE(@@global.secure_file_priv, '\\', '/'), 'trigger_', TableName, '.sql"');
SET @strSql = CONCAT('SELECT "DROP TRIGGER IF EXISTS `sft_test`.`trigger_', TableName, '_insert`;
DELIMITER $$
CREATE TRIGGER `sft_test`.`trigger_', TableName, '_insert` AFTER INSERT ON `', TableName, '` FOR EACH ROW BEGIN
UPDATE
`register`
SET
`last_insert` = NOW()
WHERE
`table_name` = \\"', TableName, '\\";
END;
$$
DELIMITER ;
DROP TRIGGER IF EXISTS `sft_test`.`trigger_', TableName, '_update`;
DELIMITER $$
CREATE TRIGGER `sft_test`.`trigger_', TableName, '_update` AFTER UPDATE ON `', TableName, '` FOR EACH ROW BEGIN
UPDATE
`register`
SET
`last_update` = NOW()
WHERE
`table_name` = \\"', TableName, '\\";
END;
$$
DELIMITER ;
DROP TRIGGER IF EXISTS `sft_test`.`trigger_', TableName, '_delete`;
DELIMITER $$
CREATE TRIGGER `sft_test`.`trigger_', TableName, '_delete` AFTER DELETE ON `', TableName, '` FOR EACH ROW BEGIN
UPDATE
`register`
SET
`last_delete` = NOW()
WHERE
`table_name` = \\"', TableName, '\\";
END;
$$
DELIMITER ;" INTO DUMPFILE ', @strSqlFilePath, ';');
PREPARE stmt FROM @strSql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP;
CLOSE TablesCursor;
END
我可以在程序中运行@strSqlFilePath吗?
韩国社交协会