当前位置:网站首页>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();
边栏推荐
- 为什么图片传输要使用base64编码
- TestSuite and testrunner in unittest
- Some problems and ideas of data embedding point
- Data warehouse interview question preparation
- R language uses the mutation function of dplyr package to standardize the specified data column (using mean function and SD function), and calculates the grouping mean of the standardized target varia
- redis 日常笔记
- MATLAB中tiledlayout函数使用
- 商業智能BI財務分析,狹義的財務分析和廣義的財務分析有何不同?
- Test process arrangement (3)
- R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据
猜你喜欢
sql优化之查询优化器
leetcode:6109. 知道秘密的人数【dp的定义】
【信息检索】链接分析
学内核之三:使用GDB跟踪内核调用链
Remove duplicate letters [greedy + monotonic stack (maintain monotonic sequence with array +len)]
How to package QT and share exe
[MySQL from introduction to proficiency] [advanced chapter] (V) SQL statement execution process of MySQL
为什么图片传输要使用base64编码
C# wpf 实现截屏框实时截屏功能
【MySQL从入门到精通】【高级篇】(四)MySQL权限管理与控制
随机推荐
尊重他人的行为
Gorm read / write separation (rotation)
What is the difference between Bi financial analysis in a narrow sense and financial analysis in a broad sense?
Remove duplicate letters [greedy + monotonic stack (maintain monotonic sequence with array +len)]
架构方面的进步
Leetcode T47: 全排列II
How to operate and invest games on behalf of others at sea
基于51单片机的超声波测距仪
Map of mL: Based on Boston house price regression prediction data set, an interpretable case is realized by using the map value to the LIR linear regression model
Understand chisel language thoroughly 12. Chisel project construction, operation and testing (IV) -- chisel test of chisel test
【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程
The mouse wheel of xshell/bash/zsh and other terminals is garbled (turn)
迅为IMX6Q开发板QT系统移植tinyplay
LiveData
商业智能BI财务分析,狭义的财务分析和广义的财务分析有何不同?
R language uses dplyr package group_ The by function and the summarize function calculate the mean and standard deviation of the target variables based on the grouped variables
R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用stress.col参数指定强调线的id子集的颜色(色彩)
Real time data warehouse
数据湖(十三):Spark与Iceberg整合DDL操作
One architecture to complete all tasks - transformer architecture is unifying the AI Jianghu on its own