当前位置:网站首页>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();
边栏推荐
- MySQL 5 installation and modification free
- 富文本编辑:wangEditor使用教程
- Learning projects are self-made, and growth opportunities are self created
- markdown 语法之字体标红
- The mouse wheel of xshell/bash/zsh and other terminals is garbled (turn)
- 吃透Chisel语言.12.Chisel项目构建、运行和测试(四)——Chisel测试之ChiselTest
- vscode 常用插件汇总
- Matters needing attention in overseas game Investment Agency
- [C question set] of VII
- Apple 5g chip research and development failure: continue to rely on Qualcomm, but also worry about being prosecuted?
猜你喜欢
[R language data science]: cross validation and looking back
Innovation and development of independent industrial software
Learn kernel 3: use GDB to track the kernel call chain
92.(cesium篇)cesium楼栋分层
Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]
sql优化之explain
Unittest框架中引入TestFixture
The font of markdown grammar is marked in red
Ruichengxin micro sprint technology innovation board: annual revenue of 367million, proposed to raise 1.3 billion, Datang Telecom is a shareholder
吃透Chisel语言.11.Chisel项目构建、运行和测试(三)——Chisel测试之ScalaTest
随机推荐
R语言ggplot2可视化:gganimate包创建动画图(gif)、使用anim_save函数保存gif可视化动图
Understand chisel language thoroughly 06. Chisel Foundation (III) -- registers and counters
Install and use MAC redis, connect to remote server redis
2022游戏出海实用发行策略
docker-compose公网部署redis哨兵模式
IDEA快捷键大全
Xcode 异常图片导致ipa包增大问题
Huahao Zhongtian sprint Technology Innovation Board: perte annuelle de 280 millions de RMB, projet de collecte de fonds de 1,5 milliard de Beida Pharmaceutical est actionnaire
R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据
How to package QT and share exe
QT how to detect whether the mouse is on a control
数据仓库面试问题准备
Excel quickly merges multiple rows of data
sharding key type not supported
吃透Chisel语言.12.Chisel项目构建、运行和测试(四)——Chisel测试之ChiselTest
吃透Chisel语言.11.Chisel项目构建、运行和测试(三)——Chisel测试之ScalaTest
MATLAB中tiledlayout函数使用
go语言中的文件创建,写入,读取,删除(转)
Gorm read / write separation (rotation)
海外游戏代投需要注意的