教教你怎么用存储过程往数据库里插入大量数据吧!

简单说说,如何使用Mysql的存储过程往数据库里插入大量的数据!最近业务需要,插入数据,调用接口非常慢,因此想到了使用mysql来进行插入。
存储过程(Stored Procedure)是一种在数据库中存储复杂程序,以便外部程序调用的一种数据库对象。
存储过程是为了完成特定功能的SQL语句集,经编译创建并保存在数据库中,用户可通过指定存储过程的名字并给定参数(需要时)来调用执行。
简单来说,存储过程就是数据库 SQL 语言层面的代码封装与重用

-- 若存在此存储过程 删除
drop procedure if exists myProcedure;
Delimiter $
create procedure myProcedure()
begin
	-- 定义一个变量i来控制循环次数
    declare i int default 1;
    while i <= 20 do
            Insert Into testTable(id,name,sex)
            VALUES (concat(1001+i,''),'小明','男');
            -- concat函数用于拼接数字和字符串
            set i = i+1;
    end while ;
end $
DELIMITER ;
-- 调用存储过程
call myProcedure();

猜你喜欢

转载自blog.csdn.net/fucccck_ly/article/details/108228731