• 关于Oracle存储过程中的循环那些事


    直接上代码,oracle循环都是使用游标来循环,例如:

    --创建表
    create table Text(
      id number(10),
      date_time date
    )
    
    delete from Text
    --创建存储过程循环添加100数据
    create or replace procedure sp_adddata
    is
    i int;
    v_loop int;--循环次数
    begin
      v_loop:=100;--计算需要循环次数
      i:=1;--为i赋值
      while  i<=v_loop  loop--循环退出条件
          insert into Text(id) values(i);
          i:=i+1;
      end  loop;--结束循环
    end sp_adddata;
    
    --使用游标修改时间,每次提交3条数据
    create or replace procedure sp_update
    is
    type type_index_id is table of text.id%type;
    v_index_id type_index_id;--取别名
    j int;--循环变量
    --将查询出来的数放到游标里
    cursor temp is
      select id from Text where date_time is null;
    begin
      j:=1;--为i赋值
     open temp;
      loop
        fetch temp bulk collect into v_index_id limit 3;
        --如果没有数据则退出
        exit when v_index_id.count = 0;
        --遍历数据
        forall i in v_index_id.first..v_index_id.last
          update Text set date_time=sysdate+j where id=v_index_id(i);
          commit;
           j:=j+1;--i依次加1
        end loop;
      close temp;
    end sp_update;
    --另一种方法,已下只写核心代码
    create or replace procedure sp_update1
    declare  cursor t1 is
          select id from text where date_time is null;
      begin
        for item1 in t1 loop
          dbms_output.put_line(item1.id);--输出
          end loop;
        end;
     end sp_update1;
    

      

  • 相关阅读:
    linux下apache(httpd)安装部署
    基于redis分布式缓存实现(新浪微博案例)
    python--字典
    django(四)
    django(三)
    django(二)
    django(一)
    053(八十)
    053(七十九)
    053(七十八)
  • 原文地址:https://www.cnblogs.com/f12-liugang/p/14339801.html
Copyright © 2020-2023  润新知