当前位置:网站首页>MySQL的存储过程练习题
MySQL的存储过程练习题
2022-07-04 12:51:00 【火眼猊】
练习题:
1.创建下个月的每天对应的表 user_2022_08_01、user_2022_08_2、…
预备知识
PREPARE stmt_name FROM preparable_stmt
EXECUTE stmt_name [USING @var_name [,@var_name]…]
{DEALLOCATE || DROP} PREPARE stmt_name
知识点 时间的处理
EXTRACRT(unit FROM date)截取时间的指定位置值
DATE ADD(date,INTERVAL expr unit)日期运算
LAST DAY(date) 获取日期的最后一天
YEAR(date)返回日期中的年
MONTH(date)返回日期的月
DAYOFMONTH(date)返回日
练习题答案
DELIMITER $$
CREATE PROCEDURE proc22_demo()
BEGIN
DECLARE next_year INT; -- 下个月的年份
DECLARE next_month INT; -- 下个月的月份
DECLARE next_month_day INT; -- 下个月最后一天的日期
DECLARE next_month_str VARCHAR(2); -- 下个月的月份字符串
DECLARE next_month_day_str VARCHAR(2); -- 下个月的日字符串
-- 处理每天的表名
DECLARE table_name_str VARCHAR(10);
DECLARE t_index INT DEFAULT 1;
-- 获取下个月的年份
SET next_year = YEAR(DATE_ADD(NOW(),INTERVAL 1 MONTH));
-- 获取下个月是几月
SET next_month =MONTH(DATE_ADD(NOW(),INTERVAL 1 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);
-- 拼接create sql语句
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后面不能使用局部变量
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();
边栏推荐
- LifeCycle
- Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
- QT how to detect whether the mouse is on a control
- docker-compose公网部署redis哨兵模式
- Test evaluation of software testing
- sharding key type not supported
- GCC【6】- 编译的4个阶段
- 为什么图片传输要使用base64编码
- Understand chisel language thoroughly 03. Write to the developer of Verilog to chisel (you can also see it without Verilog Foundation)
- [matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions
猜你喜欢
基于PaddleX的智能零售柜商品识别
数据仓库面试问题准备
Excel quickly merges multiple rows of data
MySQL version 8 installation Free Tutorial
Haobo medical sprint technology innovation board: annual revenue of 260million Yonggang and Shen Zhiqun are the actual controllers
Detailed index of MySQL
Unittest框架中引入TestFixture
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Unity shader learning (3) try to draw a circle
MySQL 5 installation and modification free
随机推荐
Xcode 异常图片导致ipa包增大问题
The game goes to sea and operates globally
92.(cesium篇)cesium楼栋分层
海外游戏代投需要注意的
Error in find command: paths must precede expression (turn)
go vendor 项目迁移到 mod 项目
吃透Chisel语言.04.Chisel基础(一)——信号类型和常量
Yingshi Ruida rushes to the scientific and Technological Innovation Board: the annual revenue is 450million and the proposed fund-raising is 979million
Understand chisel language thoroughly 04. Chisel Foundation (I) - signal type and constant
Matters needing attention in overseas game Investment Agency
sharding key type not supported
Gorm read / write separation (rotation)
Unittest框架之断言
2022游戏出海实用发行策略
CVPR 2022 | greatly reduce the manual annotation required for zero sample learning, and propose category semantic embedding rich in visual information (source code download)
Unity shader learning (3) try to draw a circle
【信息检索】分类和聚类的实验
Detailed index of MySQL
吃透Chisel语言.03.写给Verilog转Chisel的开发者(没有Verilog基础也可以看看)
R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据