У меня есть процедура сохранения для создания нового триггера, я не могу выполнить этот sql через PREPARE stmt
и EXECUTE
, я создаю внешний файл sql с кодом создания триггера.Могу ли я выполнить этот скрипт sql внутри хранимой процедуры?Есть инструкция, которая работает как SOURCE
в консоли MySQL?
Это моя хранимая процедура:
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 внутри процедуры?
Tks