创建一个存储过程范例

CREATE DEFINER=`root`@`localhost` PROCEDURE `test_myisam`()
begin
	declare i int default 1;
	while i < 1000
	do

 INSERT INTO `epa_monit_alarm_data_myisam` (alarm_status,
data_type,
enterprise_code,
object_id,
date,
create_time,
tanker_id,
oil_gun,
gas_liquid_ratio,
impermeability,
fluidic_resistor,
zero_pressure_tank,
vacuum_valve_condition,
critical_pressure_state,
post_processing_device_status
) 
VALUES (  0, 2, '2019080002', '202004081846332005', '2020-04-08 18:46:33', '2020-04-08 18:59:09', '04', '01', '0', NULL, NULL, NULL, NULL, NULL, NULL);
  
		set i=i+1;
	end while;
	commit;
end

调用存储过程,可以界面化调用,也可以执行如下命令即可:

call test_myisam();

如图这个demo,可以用来实现批量插入数据,做一些测试之类的,批量插入1000次。

存储过程用法示例

批量构造UUID

先创建一个接收uuid的测试表:

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for test_uuid
-- ----------------------------
DROP TABLE IF EXISTS `test_uuid`;
CREATE TABLE `test_uuid`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `str` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '主键',
  `create_date` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 111 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

再创建一个批量插入uuid的测试存储过程:

CREATE DEFINER=`root`@`localhost` PROCEDURE `test_innodb`()
begin
	declare i int default 1;
	while i < 100
	do
   
	 -- 批量生成uuid的测试数据
	 INSERT INTO `test_uuid`(  `str`, `create_date`) VALUES ( (select REPLACE(uuid(), '-', '')), NOW() );

 		set i=i+1;
	end while;
	commit;
end

这样,我们每次只要执行这个存储过程,就能获得一组uuid,用来给别的测试程序使用,比如做批量插入数据等时,会比较方便。