mysql 儲存過程按日期迴圈插入

2021-09-24 09:15:01 字數 4447 閱讀 5256

參考部落格

定義與使用變數時需要注意以下幾點: 

1、 declare語句必須用在degin…end語句塊中,並且必須出現在degin…end語句塊的最前面,即出現在其他語句之前。 

2、declare定義的變數的作用範圍僅限於declare語句所在的degin…end塊內及巢狀在該塊內的其他degin…end塊。 

3、儲存過程中的變數名不區分大小寫。 

set foreign_key_checks=0;

-- ----------------------------

-- procedure structure for insert_stock_n_one

-- ----------------------------

drop procedure if exists `insert_stock_n_one`;

delimiter ;;

create definer=`root`@`%` procedure `insert_stock_n_one`()

begin

-- 定義開始迴圈時間變數

declare nowdate date default now();

-- 結束時間迴圈變數

declare endtmp date default now();

-- 設定儲存過程名字

set @procedure_name = 'insert_stock_n_one';

-- 儲存過程開始執行日誌

call sp_log(@procedure_name, '***報表插入', 0, 0, null, 'start', @logid);

-- 開啟sql批量更新開關

set sql_safe_updates=0;

-- 取出上次交易處理的最大時間戳

select max(max_timestamp) into @maxtimestamp from wx_mini_procedure_exec_log

where 1=1

and procedure_name = @procedure_name;

-- 0. 引數設定

set @enddate = curdate();

-- 1. 刪除原表

truncate table wx_mini_report_product_stock_n;

-- 2. 插入截止到今天凌晨的資料

-- select * from wx_mini_report_product_stock_n;

-- 查詢最小時間 開始時間

select date_format(min(create_time),'%y%m%d') into nowdate from wx_mini_product_stock;

select date_format( max(create_time),'%y%m%d') into endtmp from wx_mini_product_stock;

-- set nowdate = date_format(begindate,'%y%m%d');

-- set endtmp = date_format(enddate,'%y%m%d');

while nowdate <= endtmp

do insert into wx_mini_report_product_stock_n (stockid, stockdetailid, regionid, regionname,

provinceid, provincename, cityid, cityname, shopid, shopcode, shopname, pcid, pccode, pcname, pcsid, pcscode, pcsname, pclid, pchid,

custgroupname, reportmonth, reportdate, createtime, pctype, alarinfo, productname,

productnum, pronumprice, productprice, sixmonthexpnum, productpricesixmonth, proalarinfo)

select

sd.stock_id stockjoinid,

sd.id stockdetailid,

r.id regionid,

r.name regionname,

p.id provinceid,

p.name provincename,

c.id cityid,

c.name cityname,

s.report_shop_id as shopid,

si.vendershopid as shopcode,

si.shopname as shopname,

s.promoter_id as pcid,

pa.account as pccode,

pa.realname as pcname,

pa.parent_promoter_id as pcsid,

ppa.account as pcscode,

ppa.realname as pcsname,

ppa.parent_promoter_id as pclid,

pcl.parent_promoter_id as pchid,

si.custgroup as custgroupname,

s.title as reportmonth,

s.report_date as reportdate,

now(),

pp.pc_type as pctype,

s.alar_info,

pd.name productname,

ifnull(sd.product_num ,0) productnum,

ifnull(sd.product_num * pd.sale_price ,0) pronumprice,

ifnull(pd.sale_price ,0) productprice,

ifnull(sd.six_month_exp_num ,0) sixmonthexpnum,

ifnull( sd.six_month_exp_num * pd.sale_price ,0) productpricesixmonth,

sd.alar_info alarinfo

from

shop_info si

left join product pd on 1=1

join stock s on s.report_shop_id=si.id

left join stock_detail sd on sd.product_id=pd.id and sd.stock_id=s.id

left join account pa on s.promoter_id = pa.promoter_id

left join profile pp on pa.promoter_profile_id = pp.id

left join region p on pp.province_id = p.id

left join region c on pp.city_id = c.id

left join region r on pp.region_id = r.id

left join account ppa on pa.parent_promoter_id = ppa.promoter_id

left join account pcl on ppa.parent_promoter_id = pcl.promoter_id

where date(s.create_time) = nowdate

order by sd.id desc;

-- 給日期+1天

set nowdate = date_add(nowdate,interval 1 day);

end while;

-- 迴圈結束

-- 3. 更新最大的時間戳 總數

select

@enddate, count(1) into @max_timestamp_new,@sql_update_count

from stock s

where 1=1

; -- 5.儲存過程開始執行日誌

if @max_timestamp_new is null then

set @max_timestamp_new = @maxtimestamp;

end if;

-- 關閉sql批量更新開關

set sql_safe_updates=1;

-- 更新日誌

call sp_log(@procedure_name, '***報表插入成功', @sql_update_count, @sql_update_count, @max_timestamp_new, 'end', @logid);

end;;

delimiter ;

mysql按日期查詢資料 mysql按日期查詢資料

問題 mysql按日期查詢乙個月內的資料,查詢返回時結果集中只顯示有資料的結果 需求 查詢結果中假如當天沒有資料,自動填零並輸出 事件描述 sql語句 select date format date added,m.d as day,count product id as total from ht...

mysql儲存過程之迴圈

1.客戶端建立乙個儲存過程,過程名稱為insert corp loop 2.填寫內容 delimiter drop procedure if exists insert corp loop create definer procedure insert corp loop in loop time ...

mysql 儲存過程 迴圈修改

mysql 迴圈修改 儲存過程 delimiter create procedure my proc begin declare billid int declare moneyorder decimal 10,2 declare stop int default 0 declare my curs...