MySQL stored procedure usage example

Create a stored procedure example


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

To call a stored procedure, you can call it on an interface, or you can execute the following commands:

call test_myisam();

As shown in the demo, it can be used to insert data in batches, do some tests, and insert 1000 times in batches.

Example of stored procedure usage

Bulk UUID construction

First create a test table to receive 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;

Create a test stored procedure that inserts uuid in batches:


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

In this way, every time we execute this stored procedure, we can obtain a set of uuids, which can be used for other test programs, such as batch insertion of data, which is more convenient.

Guess you like

Origin blog.csdn.net/Stephanie_1/article/details/105530239