开发自测时,数据库需要造大量数据时,要用到Mysql存储过程相关知识,下面分享下,希望对小伙伴们有所帮助:
CREATE PROCEDURE test() #创建存储函数; BEGIN DECLARE i INT DEFAULT 1; WHILE i < 5000 DO #下面的insert语法要改成自己的表,同时构造value.id,要考虑防重; INSERT INTO `wbms_1`.`waybill_weight_volume_2`(`id`, `tenant_id`, `waybill_code`, `operate_code`, `volume`, `weight`, `length`, `width`, `height`, `weight_time`, `volume_time`, `ope_type`, `operator_code`, `operator_name`, `operate_station_code`, `operate_station`, `is_delete`, `create_time`, `update_time`, `create_user`, `update_user`, `ts`) VALUES (1356063857646284800+i, 'test', 'ZY1000502218621', 'ZY1000502218621-1-2-', 55.0000, 55.0000, 5.5000, 55.0000, 55.0000, '2020-09-20 16:39:56', '2020-09-20 16:39:56', 3, '21180252', '陈青山', '308766', '南京江骏营业部', 0, '2021-02-01 10:16:32', '2021-02-01 10:16:32', NULL, NULL, '2021-02-01 10:16:32.000'); SET i = i+1; end WHILE; END; CALL test();#调用存储函数
下面是我写的存储过程:
CREATE PROCEDURE wmtest1(); #创建存储过程循环执行,实现插入多条; BEGIN DECLARE i INT DEFAULT 275; WHILE i<276 DO INSERT INTO `msg_org_config` VALUES(i,2,'001','工单消息'), (i,3,'001','一般告警消息'), (i,4,'001','紧急告警消息'), (i,5,'001','重要告警消息'); SET i=i+1 END WHILE; END; CALL wmtest1();#调用存储函数
注:存储过程一定要写在存储过程窗口里面,不能写在mysql的查询窗口里面,否则会报错的。
调用可以在查询窗口中。
执行插入语句
INSERT INTO `org_table` ('org_id','push_way','target_ids','rela_org','push_config') VALUES (246,'02','',246,''),
(246,'01','',246,'2,3,5,4'),
(246,'01','124,170,188,227,228,270,271,272,286,287,288',0,'2,3,5,4'),
(246,'01','129,130,247,261,324,343,350',28,'2,3,5,4');
一直报错,这里面有个自增的ID,所以我就用了字段,为什么呢?查了半天才 知道,这个字段不能加引号。mysql里加了引号就会报错。
正确的如下:
INSERT INTO `org_table` (org_id,push_way,target_ids,rela_org,push_config) VALUES (246,'02','',246,''),
(246,'01','',246,'2,3,5,4'),
(246,'01','124,170,188,227,228,270,271,272,286,287,288',0,'2,3,5,4'),
(246,'01','129,130,247,261,324,343,350',28,'2,3,5,4');