当前位置:网站首页>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();
边栏推荐
- MATLAB中tiledlayout函数使用
- Unity shader learning (3) try to draw a circle
- Data warehouse interview question preparation
- Idea shortcut keys
- Install and use MAC redis, connect to remote server redis
- Leetcode T47: 全排列II
- LiveData
- How to operate and invest games on behalf of others at sea
- 【信息检索】链接分析
- 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
猜你喜欢
[MySQL from introduction to proficiency] [advanced chapter] (V) SQL statement execution process of MySQL
NowCoder 反转链表
聊聊保证线程安全的 10 个小技巧
Rich text editing: wangeditor tutorial
C # WPF realizes the real-time screen capture function of screen capture box
Intelligence d'affaires bi analyse financière, analyse financière au sens étroit et analyse financière au sens large sont - ils différents?
富文本编辑:wangEditor使用教程
【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程
Understand chisel language thoroughly 06. Chisel Foundation (III) -- registers and counters
测试流程整理(2)
随机推荐
R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graph, and uses the by parameter to specify the groupin
Abnormal value detection using shap value
【云原生】我怎么会和这个数据库杠上了?
Product identification of intelligent retail cabinet based on paddlex
Migration from go vendor project to mod project
【MySQL从入门到精通】【高级篇】(四)MySQL权限管理与控制
Leetcode T48:旋转图像
如何游戏出海代运营、游戏出海代投
R语言ggplot2可视化:gganimate包创建动画图(gif)、使用anim_save函数保存gif可视化动图
GCC [6] - 4 stages of compilation
LifeCycle
R language ggplot2 visualization: gganimate package creates animated graph (GIF) and uses anim_ The save function saves the GIF visual animation
golang fmt. Printf() (turn)
测试流程整理(3)
Intelligence d'affaires bi analyse financière, analyse financière au sens étroit et analyse financière au sens large sont - ils différents?
92.(cesium篇)cesium楼栋分层
Understand chisel language thoroughly 04. Chisel Foundation (I) - signal type and constant
使用CLion编译OGLPG-9th-Edition源码
【FAQ】華為帳號服務報錯 907135701的常見原因總結和解决方法
ML:SHAP值的简介、原理、使用方法、经典案例之详细攻略