赞
踩
- #如果存在则删除存储过程
- DROP PROCEDURE IF EXISTS usp_generate_order_data;
- #指定分隔符为//
- DELIMITER //
- #创建存储过程
- CREATE PROCEDURE usp_generate_order_data()
- BEGIN
- #如果存在则删除该临时表
- DROP TABLE IF EXISTS tmp_sales_order;
- #创建临时表,取sales_order的表结构
- #where 1=0 表示只取表结构,不需要数据
- CREATE TABLE tmp_sales_order AS SELECT * FROM sales_order WHERE 1=0;
- SET @start_date := UNIX_TIMESTAMP('2018-1-1');
- SET @end_date := UNIX_TIMESTAMP('2018-11-23');
- SET @i := 1;
- WHILE @i<=100000 DO
- SET @customer_number := FLOOR(1+RAND()*6);
- SET @product_code := FLOOR(1+RAND()* 3);
- SET @order_date := FROM_UNIXTIME(@start_date+RAND()*(@end_date-@start_date));
- SET @amount := FLOOR(1000+RAND()*9000);
- INSERT INTO tmp_sales_order VALUES (@i,@customer_number,@product_code,@order_date,@order_date,@amount);
- SET @i := @i +1;
- END WHILE;
- #清空sales_order中的表数据
- TRUNCATE TABLE sales_order;
- #将临时表中的数据插入到sales_order中
- INSERT INTO sales_order
- SELECT NULL,customer_number,product_code,order_date,entry_date,order_amount
- FROM tmp_sales_order;
- COMMIT;
- #完成插入操作后需要手动删除临时表
- DROP TABLE tmp_sales_order;
- END //
- #调用存储过程
- CALL usp_generate_order_data();
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。