如何使用 Mysql 过程和 Date() 创建存档 Table
How to create an Archive Table with a Mysql Procedure and Date()
我尝试创建一个每天运行一次并存储来自更大 Table 的数据子集的过程。此 table 的名称应使用 CURDATE() 动态创建。
DROP PROCEDURE daily_backup;
DELIMITER |
CREATE PROCEDURE daily_backup()
BEGIN
SET @tbl = CONCAT('items_data_', DATE_FORMAT(CURDATE(), '%Y%m%d'));
SET @s = CONCAT('DROP TABLE IF EXISTS ', @tbl);
PREPARE stmt FROM @s;
EXECUTE stmt;
@s = CONCAT('CREATE TABLE `', @tb1, '` (`id` int(11) NOT NULL,`up` mediumint(9) NOT NULL,`down` mediumint(9) NOT NULL) ENGINE=Archive');
PREPARE stmt FROM @s;
EXECUTE stmt;
SET @s = CONCAT('INSERT INTO ', @tbl, ' SELECT id,up,down FROM items;');
PREPARE stmt FROM @s;
EXECUTE stmt;
END
这些是结果
#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 '@s = CONCAT('CREATE TABLE `', @tb1, '` (`id` int(11) NOT NULL,`up` mediumint(9) ' at line 8
编辑 // 现在工作
DROP PROCEDURE daily_backup;
DELIMITER |
CREATE PROCEDURE daily_backup()
BEGIN
SET @tbl = CONCAT('items_data_', DATE_FORMAT(CURDATE(), '%Y%m%d'));
SET @s = CONCAT('DROP TABLE IF EXISTS ', @tbl);
PREPARE stmt FROM @s;
EXECUTE stmt;
SET @s = CONCAT('CREATE TABLE `', @tbl, '` (`id` int(11) NOT NULL,`up` mediumint(9) NOT NULL,`down` mediumint(9) NOT NULL) ENGINE=Archive');
PREPARE stmt FROM @s;
EXECUTE stmt;
SET @s = CONCAT('INSERT INTO ', @tbl, ' SELECT id,up,down FROM items;');
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
变量赋值总是需要一个SET
命令:
SET @s = CONCAT('CREATE TABLE `', @tbl, '` (`id` int(11) NOT NULL,`up` mediumint(9) NOT NULL,`down` mediumint(9) NOT NULL) ENGINE=Archive');
注意你这个语句里有@tb1
,应该是@tbl
.
使用后 DEALLOCATE
准备好的语句也是一个好习惯,即
DEALLOCATE PREPARE stmt;
在EXECUTE stmt
之后
我尝试创建一个每天运行一次并存储来自更大 Table 的数据子集的过程。此 table 的名称应使用 CURDATE() 动态创建。
DROP PROCEDURE daily_backup;
DELIMITER |
CREATE PROCEDURE daily_backup()
BEGIN
SET @tbl = CONCAT('items_data_', DATE_FORMAT(CURDATE(), '%Y%m%d'));
SET @s = CONCAT('DROP TABLE IF EXISTS ', @tbl);
PREPARE stmt FROM @s;
EXECUTE stmt;
@s = CONCAT('CREATE TABLE `', @tb1, '` (`id` int(11) NOT NULL,`up` mediumint(9) NOT NULL,`down` mediumint(9) NOT NULL) ENGINE=Archive');
PREPARE stmt FROM @s;
EXECUTE stmt;
SET @s = CONCAT('INSERT INTO ', @tbl, ' SELECT id,up,down FROM items;');
PREPARE stmt FROM @s;
EXECUTE stmt;
END
这些是结果
#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 '@s = CONCAT('CREATE TABLE `', @tb1, '` (`id` int(11) NOT NULL,`up` mediumint(9) ' at line 8
编辑 // 现在工作
DROP PROCEDURE daily_backup;
DELIMITER |
CREATE PROCEDURE daily_backup()
BEGIN
SET @tbl = CONCAT('items_data_', DATE_FORMAT(CURDATE(), '%Y%m%d'));
SET @s = CONCAT('DROP TABLE IF EXISTS ', @tbl);
PREPARE stmt FROM @s;
EXECUTE stmt;
SET @s = CONCAT('CREATE TABLE `', @tbl, '` (`id` int(11) NOT NULL,`up` mediumint(9) NOT NULL,`down` mediumint(9) NOT NULL) ENGINE=Archive');
PREPARE stmt FROM @s;
EXECUTE stmt;
SET @s = CONCAT('INSERT INTO ', @tbl, ' SELECT id,up,down FROM items;');
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
变量赋值总是需要一个SET
命令:
SET @s = CONCAT('CREATE TABLE `', @tbl, '` (`id` int(11) NOT NULL,`up` mediumint(9) NOT NULL,`down` mediumint(9) NOT NULL) ENGINE=Archive');
注意你这个语句里有@tb1
,应该是@tbl
.
使用后 DEALLOCATE
准备好的语句也是一个好习惯,即
DEALLOCATE PREPARE stmt;
在EXECUTE stmt