您好,欢迎访问一九零五行业门户网

Oracle 函数中游标及递归的应用

在代码中使用递归可能大部分程序员都不陌生,但是在存储过程或者函数中写个递归估计就不多见了。今天遇到了一个在oracle函数中使
在代码中使用递归可能大部分程序员都不陌生,但是在存储过程或者函数中写个递归估计就不多见了。
今天遇到了一个在oracle函数中使用递归的例子,特记录下来,方便以后查阅
create or replace function f_func(v_pcatalogue in number, i_newpcatalogue in number, v_authtypeid in number)
  return number
  as
  v_newcatalogueid number(10);
  v_oldcatalogueid number(10);
  v_newpcatalogue number(10);
  v_count number(10);
  v_value number(10);
  --根据目录表父节点id查询目录表数据的游标
  cursor cusor_1(v_pcatalogue number) is
        select id, cname, ename, cdescription, edescription, authtype, pcatalogue, orderb, createtime, userid, creater, updatetime, updateuserid, updator, state from t_catalogue
        where authtype=v_authtypeid and pcatalogue=v_pcatalogue;
begin
  --记录下旧的父节点id
  v_oldcatalogueid := v_pcatalogue;
  --记录下新的父节点id
  v_newpcatalogue := i_newpcatalogue;
  select count(1) into v_count from t_catalogue where authtype=v_authtypeid and pcatalogue=v_pcatalogue;
  if v_count = 0 then
    return 1;
  else
    for c2 in cusor_1(v_oldcatalogueid) loop
 --生成新的id及拷贝工作
      v_oldcatalogueid := c2.id;
      select hibernate_sequence.nextval into v_newcatalogueid from dual;
      if v_newpcatalogue is null then
        select hibernate_sequence.nextval into v_newpcatalogue from dual;
      end if;
      insert into t_catalogue_20140619(id, cname, ename, cdescription, edescription, authtype, pcatalogue, orderb, createtime, userid, creater, updatetime, updateuserid, updator, state)
            values(v_newcatalogueid, c2.cname, c2.ename, c2.cdescription, c2.edescription, c2.authtype, v_newpcatalogue, c2.orderb, c2.createtime, c2.userid, c2.creater, c2.updatetime, c2.updateuserid, c2.updator, c2.state);
      commit;
      --不用变量接值,编译过不了
      v_value := f_func(v_oldcatalogueid, v_newcatalogueid, v_authtypeid);
    end loop;
    --这个返回很重要
    return 1;
  end if;
end f_func;
/
linux-6-64下安装oracle 12c笔记
在centos 6.4下安装oracle 11gr2(x64)
oracle 11gr2 在vmware虚拟机中安装步骤
debian 下 安装 oracle 11g xe r2
本文永久更新链接地址:

其它类似信息

推荐信息