• 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;

  • 相关阅读:
    MySQL实现对身份证格式验证
    基于Flink1.11的SQL构建实时数仓探索实践
    详解flink 1.11 中的CDC (Change Data Capture)
    ERROR 1093 (HY000): You can't specify target table 'xxx' for update in FROM clause 问题解决
    用Flink SQL CDC + ES实现数据实时化真香
    程序员创业及如何提高影响力
    Hive高阶函数posexplode(可以用于生成动态日期序列)
    Hive使用ORC格式存储进行优化
    数据仓库逻辑区域及各区域的功能和特点
    hive错误排查一:hive中执行 drop table命令卡住,删除表不成功
  • 原文地址:https://www.cnblogs.com/fengyiru6369/p/7200976.html
Copyright © 2020-2023  润新知