我在MySQL中编写了一个存储过程。我在将日期值从日期转换为整数值时遇到问题。我需要将Day(now())
转换为整数,因为我需要对此值进行一些操作。
请参阅下面的详细程序代码。
DROP PROCEDURE IF EXISTS `CREATE_PARTITION_FOR_MONTH`();
delimiter //
CREATE PROCEDURE `CREATE_PARTITION_FOR_MONTH`()
BEGIN
DECLARE current_month_total_days_count INT;
DECLARE current_month_days_left INT;
DECLARE current_day INT;
DECLARE next_month_first_date DATE ;
DECLARE loop_start_val INT DEFAULT 0;
DECLARE total_days_in_next_month INT;
DECLARE next_month_var_date DATE;
DECLARE partition_val VARCHAR(20) DEFAULT NULL;
DECLARE range_val INT DEFAULT 0;
SET current_month_total_days_count := CAST(DAY(LAST_DAY(now())) AS UNSIGNED);
SET current_day := CAST(EXTRACT(MONTH FROM CURDATE()) AS UNSIGNED);
SET current_month_days_left := current_month_total_days_count - current_day;
SET next_month_first_date := DATE(ADDDATE(CURDATE(),current_month_days_left));
SET total_days_in_next_month := DAY(LAST_DAY(next_month_first_date));
SET next_month_var_date := next_month_first_date;
SET loop_start_val := 0;
WHILE loop_start_val < total_days_in_next_month DO
SET next_month_var_date := DATE(ADDDATE(next_month_first_date,loop_start_val));
SET partition_val := CONCAT('p',TO_DAYS(next_month_var_date));
SET range_val := TO_DAYS(next_month_var_date);
ALTER TABLE XYZ ADD PARTITION (PARTITION partition_val VALUES LESS THAN (range_val));
SET loop_start_val := loop_start_val+1;
END WHILE;
END //;
DELIMITER ;
我收到此错误
错误1064(42000):您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在'range_val INT DEFAULT 0附近使用正确的语法; SET current_month_total_days_count = SELECT CAST(DAY(L'在第11行
答案 0 :(得分:0)
查看投掷线错误*DELCARE* range_val INT DEFAULT 0;
... DELCARE
是问题所在。应该是DECLARE
。好笑......