游标模板

郭哥给我的模板,留着以后用。

declare
  i number;
  cursor select_data_table is
    select * from 
  (
    select A.*, rownum RN 
     from (select * from sms_temp) A 
    where rownum <= 100
  )
  WHERE RN >= 0; 
  
  --声明一个游标,游标的名称是select_data_table
  rec_select_data_table select_data_table%rowtype;

begin
  i := 1; --将变量i一个初始值

  --开始读数据
  if select_data_table%isopen then
    close select_data_table;
  end if;
  open select_data_table;
  loop
    fetch select_data_table
      into rec_select_data_table;
    if select_data_table%notfound then
      close select_data_table;
      exit;
    else
    
      DBMS_OUTPUT.put_line('手机号码:' || rec_select_data_table.phone ||
                           ',姓名:' || rec_select_data_table.name);
      insert into sms_info_down_batch
        (batch_id)
      values
        (seq_sms_down_batch_batch_id.nextval);
      insert into sms_info_down_detail
        (id, batch_id, phone, content)
      values
        (seq_sms_down_detail_id.nextval,
         seq_sms_down_batch_batch_id.currval,
         rec_select_data_table.phone,
         '短信' || to_char(i, '000') || '内容');
    
      commit;
    
    end if;
    i := i + 1;
  end loop;

EXCEPTION
  WHEN OTHERS THEN
    dbms_output.put_line('系统出错的日志为:' || to_char(SQLERRM));    
    rollback;
END;
/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值