zoukankan      html  css  js  c++  java
  • Oracle游标—for、loop、if结合应用

    https://blog.csdn.net/akkzhjj/article/details/45397423

    1. declare  
    2.        --定义类型  
    3.        cursor t_tea  
    4.        is  
    5.        select USER_ID from T_TEACHING GROUP BY USER_ID;  
    6.        --定义一个游标变量  
    7.        t_row t_tea%rowtype;  
    8.        --定义一个number类型的临时变量  
    9.     v_count number;  
    10.  begin  
    11.       for t_row in t_tea loop   
    12.     select count(*) into v_count from T_TEACHING where USER_ID=t_row.USER_ID and COURSE_ID=02;  
    13.      if v_count = 0 then  
    14.         insert into T_TEACHING(COURSE_ID,USER_ID) values (02,t_row.USER_ID);  
    15.      end if;  
    16.        end loop;  
    17. end;  

    oracle for loop循环以及游标循环

    1. for in loop形式

      DECLARE
         CURSOR c_sal IS SELECT employee_id, first_name || last_name ename, salary
         FROM employees ;
      BEGIN
         --隐含打开游标
         FOR v_sal IN c_sal LOOP
         --隐含执行一个FETCH语句
            DBMS_OUTPUT.PUT_LINE(to_char(v_sal.employee_id)||'---'|| v_sal.ename||'---'||to_char(v_sal.salary)) ;
         --隐含监测c_sal%NOTFOUND
         END LOOP;
      --隐含关闭游标
      END;

    2.普通的游标循环

      declare
      --定义游标并且赋值(is 不能和cursor分开使用)
      cursor stus_cur is select from students;
      --定义rowtype
      cur_stu students%rowtype;
      /*开始执行*/
      begin
      --开启游标
      open stus_cur;
       --loop循环
       loop
       --循环条件
       exit when stus_cur%notfound;
       --游标值赋值到rowtype
       fetch stus_cur into cur_stu;
       --输出
       dbms_output.put_line(cur_stu.name);
       --结束循环
       end loop;
      --关闭游标
      close stus_cur;
      /*结束执行*/
     end;
    3.高效的游标循环
      declare
      cursor myemp_cur
      is select from myemp;
      type myemp_tab is table of myemp%rowtype;
      myemp_rd myemp_tab;
      begin
       open myemp_cur;
       loop
       fetch myemp_cur bulk collect into myemp_rd limit 20;
       for in 1..myemp_rd.count loop
        dbms_output.put_line('姓名:'||myemp_rd(i).ename);
       end loop;
       exit when myemp_cur%notfound;
       end loop;
      end;
     
    BULK COLLECT 子句会批量检索结果,即一次性将结果集绑定到一个集合变量中,并从SQL引擎发送到PL/SQL引擎。通常可以在SELECT INTO、
    FETCH INTO以及RETURNING INTO子句中使用BULK COLLECT.
    BULK COLLECT的限制
    1、不能对使用字符串类型作键的关联数组使用BULK COLLECT 子句。
    2、只能在服务器端的程序中使用BULK COLLECT,如果在客户端使用,就会产生一个不支持这个特性的错误。
    3、BULK COLLECT INTO 的目标对象必须是集合类型。
    4、复合目标(如对象类型)不能在RETURNING INTO 子句中使用。
    5、如果有多个隐式的数据类型转换的情况存在,多重复合目标就不能在BULK COLLECT INTO 子句中使用。
    6、如果有一个隐式的数据类型转换,复合目标的集合(如对象类型集合)就不能用于BULK COLLECTINTO 子句中。
  • 相关阅读:
    Serialization and deserialization are bottlenecks in parallel and distributed computing, especially in machine learning applications with large objects and large quantities of data.
    Introduction to the Standard Directory Layout
    import 原理 及 导入 自定义、第三方 包
    403 'Forbidden'
    https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
    These interactions can be expressed as complicated, large scale graphs. Mining data requires a distributed data processing engine
    mysqldump --flush-logs
    mysql dump 参数
    mysql dump 参数
    如果是在有master上开启了该参数,记得在slave端也要开启这个参数(salve需要stop后再重新start),否则在master上创建函数会导致replaction中断。
  • 原文地址:https://www.cnblogs.com/xuhewei/p/9243961.html
Copyright © 2011-2022 走看看