当前位置:网站首页>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();
边栏推荐
- File creation, writing, reading, deletion (transfer) in go language
- IP lab monthly resumption · issue 5
- 递增的三元子序列[贪心训练]
- 吃透Chisel语言.12.Chisel项目构建、运行和测试(四)——Chisel测试之ChiselTest
- GCC【6】- 编译的4个阶段
- 392. Judgement subsequence
- QT how to detect whether the mouse is on a control
- 吃透Chisel语言.07.Chisel基础(四)——Bundle和Vec
- Vscode common plug-ins summary
- sql优化之查询优化器
猜你喜欢

Test process arrangement (2)

Unittest框架中引入TestFixture
![[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions](/img/7a/9b559313b407f9a12cbaed7bebd4dc.png)
[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions

gin集成支付宝支付

基于YOLOv1的口罩佩戴检测

docker-compose公网部署redis哨兵模式

Unittest中的TestSuite和TestRunner

How to package QT and share exe

吃透Chisel语言.10.Chisel项目构建、运行和测试(二)——Chisel中生成Verilog代码&Chisel开发流程

Rich text editing: wangeditor tutorial
随机推荐
Gorm data insertion (transfer)
如何游戏出海代运营、游戏出海代投
Test process arrangement (3)
海外游戏代投需要注意的
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
sql优化之查询优化器
R语言ggplot2可视化:gganimate包创建动画图(gif)、使用anim_save函数保存gif可视化动图
Matters needing attention in overseas game Investment Agency
Basic mode of service mesh
Unity shader learning (3) try to draw a circle
Golang 使用 JSON unmarshal 数字到 interface{} 数字变成 float64 类型(转)
How to operate and invest games on behalf of others at sea
测试流程整理(3)
【FAQ】華為帳號服務報錯 907135701的常見原因總結和解决方法
吃透Chisel语言.03.写给Verilog转Chisel的开发者(没有Verilog基础也可以看看)
学内核之三:使用GDB跟踪内核调用链
Excel快速合并多行数据
LifeCycle
Test process arrangement (2)
Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators