mysql存储过程输出九九乘法表

本文展示了如何使用MySQL存储过程动态生成九九乘法表,并将其存储到test3表中,通过循环和条件判断完成乘法表的计算与填充。最后,展示生成的乘法表格并清理表数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

长夜漫漫,无心睡眠。就写个九九表玩玩吧

delimiter // ##将语句的结束符号从分号;临时改为两个//(可以是自定义)
create procedure t11() ##声明创建存储过程
begin #存储过程开始
declare a int; #声明变量类型
declare b int;
declare c Nvarchar(1000);
create table if not exists test3(id int,id1 int,id2 int,id3 int,id4 int,id5 int,id6 int,id7 int,id8 int,id9 int);
truncate table test3; #是为了在第二次调用存储过程时将前面的数据清空
set a=1;
while a<=9 do
set b=1;
insert into test3 values(a,0,0,0,0,0,0,0,0,0);
while b<=a do
set c =concat(‘id’,b);
set @sqlStr=concat('update test3 set ‘,c,’ = ‘,a*b,’ where id = ',a);
PREPARE msql from @sqlStr; #声明预处理语句
EXECUTE msql; #调用执行预处理语句
set b=b+1;
end while;
set a=a+1;
end while;
select id1,id2,id3,id4,id5,id6,id7,id8,id9 from test3;
truncate table test3;
end// #存储过程结束
delimiter ; ###将语句的结束符号改回分号

call t11();
±-----±-----±-----±-----±-----±-----±-----±-----±-----+
| id1 | id2 | id3 | id4 | id5 | id6 | id7 | id8 | id9 |
±-----±-----±-----±-----±-----±-----±-----±-----±-----+
| 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2 | 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 3 | 6 | 9 | 0 | 0 | 0 | 0 | 0 | 0 |
| 4 | 8 | 12 | 16 | 0 | 0 | 0 | 0 | 0 |
| 5 | 10 | 15 | 20 | 25 | 0 | 0 | 0 | 0 |
| 6 | 12 | 18 | 24 | 30 | 36 | 0 | 0 | 0 |
| 7 | 14 | 21 | 28 | 35 | 42 | 49 | 0 | 0 |
| 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 0 |
| 9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 |
±-----±-----±-----±-----±-----±-----±-----±-----±-----+

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值