当前位置:网站首页>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();
边栏推荐
- Rich text editing: wangeditor tutorial
- redis 日常笔记
- sql优化之explain
- 数据仓库面试问题准备
- 【云原生】我怎么会和这个数据库杠上了?
- MySQL的触发器
- ViewModel 初体验
- The implementation of OSD on rk1126 platform supports color translucency and multi-channel support for Chinese
- Unity shader learning (3) try to draw a circle
- File creation, writing, reading, deletion (transfer) in go language
猜你喜欢

Test evaluation of software testing

Real time data warehouse

Oppo find N2 product form first exposure: supplement all short boards

Learn kernel 3: use GDB to track the kernel call chain

sql优化之查询优化器

【FAQ】華為帳號服務報錯 907135701的常見原因總結和解决方法

Excel快速合并多行数据
![Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]](/img/af/a1dcba6f45eb4ccc668cd04a662e9c.png)
Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]

【信息检索】链接分析

C # WPF realizes the real-time screen capture function of screen capture box
随机推荐
【算法leetcode】面试题 04.03. 特定深度节点链表(多语言实现)
MySQL的存储过程练习题
R language dplyr package summary_ If function calculates the mean and median of all numerical data columns in dataframe data, and summarizes all numerical variables based on conditions
R语言dplyr包summarise_if函数计算dataframe数据中所有数值数据列的均值和中位数、基于条件进行数据汇总分析(Summarize all Numeric Variables)
数据埋点的一些问题和想法
Common content type correspondence table
R语言ggplot2可视化:gganimate包创建动画图(gif)、使用anim_save函数保存gif可视化动图
[R language data science]: cross validation and looking back
ViewModel 初体验
How to package QT and share exe
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 daily notes
vscode 常用插件汇总
一种架构来完成所有任务—Transformer架构正在以一己之力统一AI江湖
Learn kernel 3: use GDB to track the kernel call chain
C# wpf 实现截屏框实时截屏功能
Fs4059c is a 5V input boost charging 12.6v1.2a. Inputting a small current to three lithium battery charging chips will not pull it dead. The temperature is 60 ° and 1000-1100ma is recommended
codeforce:C. Sum of Substrings【边界处理 + 贡献思维 + 灵光一现】
数据仓库面试问题准备