当前位置:网站首页>MySQL stored procedure exercise
MySQL stored procedure exercise
2022-07-04 14:23:00 【Fire eye Dragon】
Exercises :
1. Create a table corresponding to each day of the next month user_2022_08_01、user_2022_08_2、…
Preliminary knowledge
PREPARE stmt_name FROM preparable_stmt
EXECUTE stmt_name [USING @var_name [,@var_name]…]
{DEALLOCATE || DROP} PREPARE stmt_name
Knowledge point Time processing
EXTRACRT(unit FROM date) The specified location value of the interception time
DATE ADD(date,INTERVAL expr unit) Date operations
LAST DAY(date) Get the last day of the date
YEAR(date) Returns the year in the date
MONTH(date) Returns the month of the date
DAYOFMONTH(date) Return day
The answer to the exercise
DELIMITER $$
CREATE PROCEDURE proc22_demo()
BEGIN
DECLARE next_year INT; -- The year of the next month
DECLARE next_month INT; -- Month of next month
DECLARE next_month_day INT; -- The date of the last day of the next month
DECLARE next_month_str VARCHAR(2); -- The month string of the next month
DECLARE next_month_day_str VARCHAR(2); -- Next month's Day string
-- Process daily table names
DECLARE table_name_str VARCHAR(10);
DECLARE t_index INT DEFAULT 1;
-- Get the year of the next month
SET next_year = YEAR(DATE_ADD(NOW(),INTERVAL 1 MONTH));
-- Get what month is next
SET next_month =MONTH(DATE_ADD(NOW(),INTERVAL 1 MONTH));
-- What is the last day of next month
SET next_month_day = DAYOFMONTH(LAST_DAY(DATE_ADD(NOW(),INTERVAL 1 MONTH)));
IF next_month < 10
THEN SET next_month_str = CONCAT('0',next_month);
ELSE
SET next_month_str = CONCAT('',next_month);
END IF;
WHILE t_index <= next_month_day DO
IF (t_index < 10)
THEN SET next_month_day_str = CONCAT('0',t_index);
ELSE
SET next_month_day_str = CONCAT('',t_index);
END IF;
SET table_name_str = CONCAT(next_year,'_',next_month_str,'_',next_month_day_str);
-- Splicing create sql sentence
SET @create_table_sql = CONCAT(
'create table user_',
table_name_str,
'(`uid` INT ,`uname` varchar(50) ,`information` varchar(50)) COLLATE=\'utf8_general_ci\' ENGINE=InnoDB');
-- FROM Local variables cannot be used later
PREPARE create_table_stmt FROM @create_table_sql;
EXECUTE create_table_stmt;
DEALLOCATE PREPARE create_table_stmt;
SET t_index = t_index + 1;
END WHILE;
END $$
DELIMITER ;
CALL proc22_demo();
边栏推荐
猜你喜欢
数据湖(十三):Spark与Iceberg整合DDL操作
What is the difference between Bi financial analysis in a narrow sense and financial analysis in a broad sense?
测试流程整理(2)
C# wpf 实现截屏框实时截屏功能
MATLAB中tiledlayout函数使用
Data center concept
vscode 常用插件汇总
Understand chisel language thoroughly 10. Chisel project construction, operation and testing (II) -- Verilog code generation in chisel & chisel development process
迅为IMX6Q开发板QT系统移植tinyplay
Leetcode T48:旋转图像
随机推荐
Opencv3.2 and opencv2.4 installation
去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
GCC【6】- 编译的4个阶段
[FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions
迅为IMX6Q开发板QT系统移植tinyplay
Chapter 17 process memory
商業智能BI財務分析,狹義的財務分析和廣義的財務分析有何不同?
Can mortgage with housing exclude compulsory execution
Excel快速合并多行数据
PHP log debugging
R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用stress.col参数指定强调线的id子集的颜色(色彩)
Install and use MAC redis, connect to remote server redis
【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程
统计php程序运行时间及设置PHP最长运行时间
[MySQL from introduction to proficiency] [advanced chapter] (V) SQL statement execution process of MySQL
R language ggplot2 visualization: gganimate package creates animated graph (GIF) and uses anim_ The save function saves the GIF visual animation
基于51单片机的超声波测距仪
10.(地图数据篇)离线地形数据处理(供Cesium使用)
What is the difference between Bi financial analysis in a narrow sense and financial analysis in a broad sense?
MySQL的触发器