将外部sql文件加载到存储过程中

guykilcj  于 2021-06-23  发布在  Mysql
关注(0)|答案(0)|浏览(261)

我有一个存储过程来创建一个新的触发器,我不能通过 PREPARE stmt 以及 EXECUTE ,我用触发器创建代码创建了一个外部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公司

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题