我是靠谱客的博主 淡淡外套,这篇文章主要介绍MySQL创建存储过程,使用while循环插入数据,现在分享给大家,希望可以做个参考。

// 删除已有的user表
DROP TABLE IF EXISTS `user`;
// 创建新的user表
CREATE TABLE `user` (
`userId` INT (11) NOT NULL AUTO_INCREMENT,
`userLoginAccount` INT (255) NOT NULL,
`userPassword` VARCHAR (255) DEFAULT '1234',
`userName` INT (255) DEFAULT NULL,
`userAge` INT (25) DEFAULT NULL,
PRIMARY KEY (`userId`),
UNIQUE KEY `userLoginAccount` (`userLoginAccount`)
) ENGINE = INNODB AUTO_INCREMENT = 11 DEFAULT CHARSET = utf8;
// 删除已有的存储过程
DROP PROCEDURE IF EXISTS insertdata;
// 查阅user表
desc user; 

  • 定义存储过程
// 定义结束符为 $$
delimiter $$
create procedure insertdata()
// 创建新的存储过程
-> begin
-> declare i int default 0;
// 变量声明
-> while i < 10 do
// 循环体
// 插入数据
-> INSERT INTO user (userLoginAccount,userPassword,userName,userAge) VALUE (i , '4321' ,
i , 5 ) ;
-> set i = i + 1;
// 迭代条件
-> end while;
// 结束循环
-> end $$
// 结束存储过程
  • 调用存储过程
// 重定义结束符
delimiter;
// 调用存储过程
call insertdata();
// 查看存储数据
select * from user;

创建带参数的存储过程

create PROCEDURE insertToUserMessage (num int)
begin
declare i int default 0;
WHILE i < num do
INSERT INTO osmcs_user_message (message_type,title ,from_user_id ,to_user_id,send_time ,read_time,reply_to_comment ,reply_to_message,is_delete,is_read,file_id,file_name,file_path,file_size,soft_id,soft_name)
VALUES (6, '@admin;再次确认', 1, 1, now(), now(), 549, null, 0, 0, null, null, null, null, 483, 'Linux');
SET
i = i + 1;
END while;
end ;
call insertToUserMessage(50000);

最后

以上就是淡淡外套最近收集整理的关于MySQL创建存储过程,使用while循环插入数据的全部内容,更多相关MySQL创建存储过程内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(134)

评论列表共有 0 条评论

立即
投稿
返回
顶部