当前位置:网站首页>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();
边栏推荐
- Blob, text geometry or JSON column'xxx'can't have a default value query question
- 【云原生】我怎么会和这个数据库杠上了?
- gin集成支付宝支付
- 按照功能对Boost库进行分类
- 【MySQL从入门到精通】【高级篇】(四)MySQL权限管理与控制
- R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布
- Install MySQL
- LifeCycle
- R语言使用lattice包中的bwplot函数可视化箱图(box plot)、par.settings参数自定义主题模式
- C# wpf 实现截屏框实时截屏功能
猜你喜欢
![去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]](/img/af/a1dcba6f45eb4ccc668cd04a662e9c.png)
去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]

Test process arrangement (3)

数据湖(十三):Spark与Iceberg整合DDL操作

Product identification of intelligent retail cabinet based on paddlex

CVPR 2022 | greatly reduce the manual annotation required for zero sample learning, and propose category semantic embedding rich in visual information (source code download)

Visual Studio调试方式详解

迅为IMX6Q开发板QT系统移植tinyplay

【信息检索】分类和聚类的实验
![[FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions](/img/43/1a9786c89a5ab21d1fb8903cb7b77e.png)
[FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions

聊聊保证线程安全的 10 个小技巧
随机推荐
flink sql-client.sh 使用教程
Apple 5g chip research and development failure: continue to rely on Qualcomm, but also worry about being prosecuted?
Understand chisel language thoroughly 10. Chisel project construction, operation and testing (II) -- Verilog code generation in chisel & chisel development process
MySQL的存储过程练习题
Mask wearing detection based on yolov1
Some problems and ideas of data embedding point
Leetcode T48:旋转图像
Detailed index of MySQL
Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
IP lab monthly resumption · issue 5
Ruiji takeout notes
10.(地图数据篇)离线地形数据处理(供Cesium使用)
QT how to detect whether the mouse is on a control
C # WPF realizes the real-time screen capture function of screen capture box
nowcoder重排链表
redis 日常笔记
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
实时数据仓库
Why should Base64 encoding be used for image transmission
Rich text editing: wangeditor tutorial