My sql之存储过程+游标

sql 实例如下:

/**************定义更改car_station_user_acct_his new_balance old_balance存储过程**************/
create procedure abc (in number varchar(256))  -- 【in表示这个参数是传入参数,out表示这个是传出参数(类似Java中的return),in out表示这个既是传入,又是传出参数,可以利用它传入该存储过程,然后接到处理后的这个参数】

begin
-- 定义变量
declare v_date,v_name varchar(256);


-- 申明 游标
declare cur cursor for select create_date,update_by from car_order where bill_number = number;

-- 遍历数据结束标志
declare done int default false;

-- 将结束标志绑定到游标

declare continue handler for not found set done = true;

/* select a.create_date,a.user_name
from (select h.create_date,h.user_name,
-- 对笔数进行判断,算出相应工分
case when h.trade_no < 11 then round(convert(o.total,DECIMAL)*0.3,0)
when h.trade_no < 21 then round(convert(o.total,DECIMAL)*0.4,0)
else round(CONVERT(o.total,DECIMAL)*0.5,0) end as car
from car_order o, car_station_user_acct_his h
where o.bill_number = h.bill_number and h.trade_no is not NULL and o.total > 1 and o.update_by = '2A36006' ) a
where a.car != a.deduct; */

-- 打开游标、开始循环
open cur;
read_loop:loop
fetch cur into v_date,v_name;
if done then
leave read_loop;
end if;

--过程调试
-- step1 修改用户积分历史记录(car_station_user_acct_his)的变换后值,从当前单据往后修改,包括当前单据
update car_station_user_acct_his
set new_balance = new_balance+1
where create_date >= v_date
and user_name = v_name;

--过程调试
-- step2 修改用户积分历史记录(car_station_user_acct_his)的变换前值,从当前单据往后修改
update car_station_user_acct_his
set old_balance = old_balance+1
where create_date > v_date
and user_name = v_name;

-- step3 修改订单记录的积分
update car_order set deduct = deduct+1
where bill_number = number
and create_date>=v_date
and create_date<DATE_ADD(date(now()),INTERVAL 1 DAY)
and update_by = v_name;

-- step4 修改用户当前积分
update car_order set deduct = deduct + 1 where bill_number = number;

end loop;
close cur;
end;

posted @ 2017-07-18 16:03  木棉花的漂泊  阅读(238)  评论(0编辑  收藏  举报