我有三张表zkf,zkf_temp,md_cost_item我要实现的目标是把zkf中的数据经过分类统计插入到zkf_temp中,md_cost_item中提供一些跟编码相关的信息。
具体业务逻辑如下:zkf中有重要字段item_id,pur_date,prod_id,ent_id。其中item_id用来区分产品,按级别不同其长度可能有9,7,5,3三种,pur_date采用turnc函数取到月份,我的操作思路是这样的:先用游标遍历zkf,对其中的每条记录的item_id都进行拆分(拆分时根据md_cost_item中的inner_level字段进行,如果inner_level为四那么要分成9,7,5,3插入到zkf_temp中,zkf_temp中不允许有这样的记录:同item_id,prod_id,pur_date,ent_id),然从zkf_temp中查询如果有相同的则更新字段qty,没有则插入一条新记录。可我的代码不能实现不知问题出在哪
create or replace procedure pro_zy is
  cursor zkf_cur1 is select * from ZKF for update;
  cur_recp zkf_cur1%rowtype;
  v_zkf_lev1 zkf%rowtype;
  v_zkf_lev2 zkf%rowtype;
  v_zkf_lev3 zkf%rowtype;
  v_zkf_lev4 zkf%rowtype;
  v_zkf_temp zkf_temp%rowtype;
  v_zkf_temp1 zkf_temp%rowtype;
  v_zkf_temp2 zkf_temp%rowtype;
  v_zkf_temp3 zkf_temp%rowtype;
  v_zkf_temp4 zkf_temp%rowtype;  ent_id zkf.ent_id%type;
  prod_id zkf.prod_id%type;
  line_no zkf.line_no%type;
  item_id zkf.item_id%type;
  qty zkf.qty%type;
  pur_date zkf.pur_date%type;
  measure zkf.measure%type;
  total_amt zkf.total_amt%type;
  manage_id zkf.manage_id%type;
--定义层次  
  v_inner_lev integer;
  v_inner_lev1 integer;
  v_inner_lev2 integer;
  v_inner_lev3 integer;
  v_inner_lev4 integer;
--剪切id  
  item_id_temp1 md_cost_item.item_id%type;
  item_id_temp2 md_cost_item.item_id%type;
  item_id_temp3 md_cost_item.item_id%type;
  item_id_temp4 md_cost_item.item_id%type;
--日期处理
  v_date1 zkf.pur_date%type;
  v_date2 zkf.pur_date%type;
--截取月和年:
  v_nyear number;
  v_nmonth number;
  --定义记录类型用于函数中存记录  
  TYPE recordRec IS RECORD(
     cost_id zkf_temp.cost_id%type,
     ent_id zkf_temp.ent_id%type,
     nyear zkf_temp.nyear%type,
     nmonth zkf_temp.nmonth%type,
     ndate zkf_temp.ndate%type,
     prod_id zkf_temp.prod_id%type,
     item_id zkf_temp.item_id%type,
     prod_sort_id zkf_temp.prod_sort_id%type,
     cost_type zkf_temp.cost_type%type,
     line_no zkf_temp.line_no%type,
     measure zkf_temp.measure%type,
     qty zkf_temp.qty%type,
     price zkf_temp.price%type,
     rmb_amt zkf_temp.rmb_amt%type,
     ass_amt zkf_temp.ass_amt%type,
     total_amt zkf_temp.total_amt%type,
     src_tyep zkf_temp.src_type%type,
     sum_type zkf_temp.sum_type%type,
     manage_id zkf_temp.manage_id%type
  );
     TYPE recordTab IS TABLE OF recordRec INDEX BY BINARY_INTEGER; 
     availableRecordTab recordTab; 
--创建函数
  function select_record(entId in varchar2,prodId in varchar2,itemId in varchar2,costType in char,sumType in char,Ndate in char) return boolean is
    begin
      select  cost_id,ent_id,nyear,nmonth,ndate,prod_id,item_id,prod_sort_id,cost_type,line_no,measure,qty,price,rmb_amt,ass_amt,total_amt,src_type,sum_type,manage_id BULK COLLECT INTO availableRecordTab from zkf_temp where (ent_id=entId)and(prod_id=prodId)and(item_id=itemId)and(cost_type=costType)and(sum_type=sumType)and(ndate=Ndate);
      return true;
    exception
      when others then
      return false;
    end;
begin
  for cur_recp in zkf_cur1 loop
    v_date1:=trunc(to_date(cur_recp.pur_date,'yyyy-MM-dd'),'month');
    v_nyear:=to_number(substr(cur_recp.pur_date,0,4));
    v_nmonth:=to_number(substr(cur_recp.pur_date,6,2));
  --找到对应的层和内部码
     select inner_level into v_inner_lev from md_cost_item where item_id=cur_recp.item_id;
     v_inner_lev1:=v_inner_lev-1;
     if (v_inner_lev1>0) then  
        select substr(item_id,0,v_inner_lev1*2+1) into item_id_temp1 from md_cost_item where item_id=cur_recp.item_id; 
--逐条赋值:
       v_zkf_lev1.ent_id:=cur_recp.ent_id;
       v_zkf_lev1.prod_id:=cur_recp.prod_id;
       v_zkf_lev1.item_id:=item_id_temp1;
       v_zkf_lev1.line_no:=cur_recp.line_no;
       v_zkf_lev1.measure:=cur_recp.measure;
       v_zkf_lev1.qty:=cur_recp.qty;
       v_zkf_lev1.own_price:=cur_recp.own_price;
       v_zkf_lev1.total_amt:=cur_recp.total_amt;
       v_zkf_lev1.manage_id:=cur_recp.manage_id;
       v_zkf_lev1.pur_date:=v_date1;
     else null;
     end if;
     v_inner_lev2:=v_inner_lev1-1;
     if (v_inner_lev2>0) then
       item_id_temp2:=substr(cur_recp.item_id,0,v_inner_lev2*2+1);
       v_zkf_lev2.ent_id:=cur_recp.ent_id;
       v_zkf_lev2.prod_id:=cur_recp.prod_id;
       v_zkf_lev2.item_id:=item_id_temp2;
       v_zkf_lev2.line_no:=cur_recp.line_no;
       v_zkf_lev2.measure:=cur_recp.measure;
       v_zkf_lev2.qty:=cur_recp.qty;
       v_zkf_lev2.own_price:=cur_recp.own_price;
       v_zkf_lev2.total_amt:=cur_recp.total_amt;
       v_zkf_lev2.manage_id:=cur_recp.manage_id;
       v_zkf_lev2.pur_date:=v_date1;
     else null;
     end if;
     v_inner_lev3:=v_inner_lev2-1;
     if (v_inner_lev3>0) then
       item_id_temp3:=substr(cur_recp.item_id,0,v_inner_lev3*2+1);
       v_zkf_lev1.ent_id:=cur_recp.ent_id;
       v_zkf_lev3.prod_id:=cur_recp.prod_id;
       v_zkf_lev3.item_id:=item_id_temp3;
       v_zkf_lev3.line_no:=cur_recp.line_no;
       v_zkf_lev3.measure:=cur_recp.measure;
       v_zkf_lev3.qty:=cur_recp.qty;
       v_zkf_lev3.own_price:=cur_recp.own_price;
       v_zkf_lev3.total_amt:=cur_recp.total_amt;
       v_zkf_lev3.manage_id:=cur_recp.manage_id;
       v_zkf_lev3.pur_date:=v_date1;
     else null;
     end if;
     v_inner_lev4:=v_inner_lev3-1;
     if (v_inner_lev4>0) then
       item_id_temp4:=substr(cur_recp.item_id,0,v_inner_lev4*2+1);
       v_zkf_lev4.ent_id:=cur_recp.ent_id;
       v_zkf_lev4.prod_id:=cur_recp.prod_id;
       v_zkf_lev4.item_id:=item_id_temp4;
       v_zkf_lev4.line_no:=cur_recp.line_no;
       v_zkf_lev4.measure:=cur_recp.measure;
       v_zkf_lev4.qty:=cur_recp.qty;
       v_zkf_lev4.own_price:=cur_recp.own_price;
       v_zkf_lev4.total_amt:=cur_recp.total_amt;
       v_zkf_lev4.manage_id:=cur_recp.manage_id;
       v_zkf_lev4.pur_date:=v_date1;
     else null;
     end if;
 
      if (SELECT_RECORD(cur_recp.ent_id,cur_recp.prod_id,cur_recp.item_id,'c','m',v_date1)=true) then
      update zkf_temp set qty=qty+cur_recp.qty where cost_id=zkf_temp.cost_id;
       else
       insert into zkf_temp (cost_id,ent_id,nyear,nmonth,ndate,prod_id,item_id,prod_sort_id,cost_type,line_no,measure,qty,price,rmb_amt,ass_amt,total_amt,src_type,sum_type,manage_id) values ((SELECT SYS_GUID() from dual),cur_recp.ent_id,v_nyear,v_nmonth,to_char(v_date1),cur_recp.prod_id,cur_recp.item_id,'xxxx','c',cur_recp.line_no,cur_recp.measure,cur_recp.qty,cur_recp.own_price,cur_recp.qty*cur_recp.own_price,0,cur_recp.total_amt,'p','m',cur_recp.manage_id);
--0     end if;
--0     if (v_inner_lev1>0)then
--0       if (SELECT_RECORD(cur_recp.ent_id,cur_recp.prod_id,cur_recp.item_id,'c','m',v_date1)=true) then
--0          update zkf_temp set qty=qty+cur_recp.qty where cost_id=zkf_temp.cost_id;
--0       else
--0         insert into zkf_temp (cost_id,ent_id,nyear,nmonth,ndate,prod_id,item_id,prod_sort_id,cost_type,line_no,measure,qty,price,rmb_amt,ass_amt,total_amt,src_type,sum_type,manage_id) values ((SELECT SYS_GUID() from dual),v_zkf_lev1.ent_id,v_nyear,v_nmonth,to_char(v_date1),v_zkf_lev1.prod_id,v_zkf_lev1.item_id,'xxxx','c',v_zkf_lev1.line_no,v_zkf_lev1.measure,v_zkf_lev1.qty,v_zkf_lev1.own_price,v_zkf_lev1.qty*v_zkf_lev1.own_price,0,v_zkf_lev1.total_amt,'p','m',v_zkf_lev1.manage_id);
--0       end if;
--0     end if;
--0     if (v_inner_lev2>0) then      end if;
      end loop;
       commit;     
     end;
--0:表示注释,内容太长,剪切掉了关于层次的部分

解决方案 »

  1.   

    1.LZ的这个函数可能拎出存贮过程之外进行编译。
    2.在pl/sql(或toad)中使用test,给参数赋初值后进行调试。
    --创建函数 
      function select_record(entId in varchar2,prodId in varchar2,itemId in varchar2,costType in char,sumType in char,Ndate in char) return boolean is 
        begin 
          select  cost_id,ent_id,nyear,nmonth,ndate,prod_id,item_id,prod_sort_id,cost_type,line_no,measure,qty,price,rmb_amt,ass_amt,total_amt,src_type,sum_type,manage_id BULK COLLECT INTO availableRecordTab from zkf_temp where (ent_id=entId)and(prod_id=prodId)and(item_id=itemId)and(cost_type=costType)and(sum_type=sumType)and(ndate=Ndate); 
          return true; 
        exception 
          when others then 
          return false; 
        end; 
      

  2.   

    各位老大,刚接触orcal不会plsql,只知道从网上这里借鉴点那里搬点,什么单步执行或是传值调试函数,俺窍不通!如果你发现了问题就直接告诉俺吧,或者说得具体点怎么调试,我急着用这东西,现去搬书学调试一时半会还搞不清!
      

  3.   

    1:我们没有测试环境[数据、表结构]
    2:我们不清楚你的业务
    3:逻辑不复杂就是字段多了些,没有数据我们看不出问题在哪
    4:只能给你些建议:首先在你的存储过程代码中设置断点->打开PL\SQL DEVELOPER工具->左边List->Procedures->找到你的存储过程名字->右键test->F9->Ctrl+N...