zoukankan      html  css  js  c++  java
  • mysql 存储过程

    注意存储过程中每句sql都要以分号结尾,不然报错。

    存储过程中变量的定义:

      局部变量:

        关键字:declare,declare 变量名 变量类型  default  默认值;

        作用范围(scope):begin...end之间有效;

        定义多个相同类型的变量:declare 变量名a,变量名b 变量类型 default 默认值;

        注意:在存储过程或函数中,declare语句要放在begin...end之间最开始的位置,否则会报错。

      会话变量:

        关键符号:@,不需要声明,会自动根据值类型来确定数据类型

        作用范围(scope):整个会话过程中有效(类似于全局变量)

     存储过程中变量的赋值:

      set方式:set 变量 = 变量值;(注意:如果等号右边是一个sql语句,sql语句需要用小括号括起来,不然会报错,且该sql只能查询一个字段,多字段时用into方式)

      into方式:select 字段a,字段b into 字段值aa,字段值bb from 表名 where 条件;

    存储过程中游标的定义与使用:

      定义:四个ceclare要放在begin...end开始的位置,且第四行定义语句不能在第三行定义语句之前。

    declare done boolean default 0;#定义遍历数据结束标志
    declare v_station varchar(50);#定义需要接收游标数据的变量
    declare cursor_product_stations cursor for select operation_code from t_dd_ppr_process_operation where process_id = v_pprid order by operation_code;#定义游标
    declare continue handler for not found set done = 1; #将结束标志绑定到游标

      

      使用:游标中数据循环有三种方式,分别未Loop循环,while循环,repeat循环

      Loop循环:

            open cursor_product_stations;#打开游标
                    read_loop:LOOP
                        fetch cursor_product_stations into v_station;#提取游标中的数据
                        if done then
                            leave read_loop;#声明何时结束循环
                        end if;
                #业务处理insert into t_dd_ct_station_status(id,product_snum,unit_code,unit_status,unit_reject_code,create_date) 
                            values (uuid(),Snum,v_station,null,null,sysdate());
                    end loop;
                close cursor_product_stations;#关闭游标

      while循环:

        open cursor_product_stations;#打开游标
                while !done do
                    fetch cursor_product_stations into v_station;#提取游标中的数据
                      if !done then
                         insert into t_dd_ct_station_status(id,product_snum,unit_code,unit_status,unit_reject_code,create_date) 
                            values (uuid(),Snum,v_station,null,null,sysdate());
                      end if;    
                end while;
           close cursor_product_stations;#关闭游标

      repeat循环:

           open cursor_product_stations;#打开游标
                repeat
                    fetch cursor_product_stations into v_station;#提取游标中的数据
                       if !done then
                          insert into t_dd_ct_station_status(id,product_snum,unit_code,unit_status,unit_reject_code,create_date) 
                            values (uuid(),Snum,v_station,null,null,sysdate());
                       end if;
                    until done
                end repeat;
           close cursor_product_stations;#关闭游标

     

    调用带输出参数的存储过程:参数个数要全,输出参数用带@符号的任意变量名代替

    CALL `tcx_12101`.`p_dl_on_line_save_snum_and_station_status`('OP1010', 'ZFXKH/QIANPU/210426001A', '', 111, @ReturnValue1);
    select @ReturnValue1;
  • 相关阅读:
    js Image对象 及rollover效果
    精通javascript:元素的尺寸
    javascript 快捷操作
    精通javascript:获取位置
    javascript对象小问题
    javascript 获取元素的真实,最终的css样式
    MySQL索引
    精通javascript:元素的可见性
    javascript 图像预载入和如何判断图片是否加载完成
    ASP.NET Ajax的CalendarExtender控件被其它Div遮住问题
  • 原文地址:https://www.cnblogs.com/luna-hehe/p/14704056.html
Copyright © 2011-2022 走看看