oracle在declare中根据查询的结果定义游标,游标的select statement部分是动态的。

来源:互联网 发布:php 开发客户端 编辑:程序博客网 时间:2024/05/21 14:18
declare


  flagScore        varchar2(10);
  flagCounts       number;
  oneflagCountsTmp number;
  tpClaimAmount    number;
  curindicatorId   number(10);
  dateOfFlag       varchar2(50);
  tmp              varchar(100);
  strSrc           varchar(4000);
  extCount         number(10) := 0;
  instanceId       number(10);
  itemId           varchar2(10);
  resultId         varchar2(10);
  strTmp           clob;
  flag             varchar2(2);
  flagCheckTime    varchar2(19);
  taskUpdateTime   varchar2(19);
  -- type curRef is ref cursor;
  num number := 1;
  --cur1 curRef;
  cursor curCase is
    select * from table1 t1
     group by t1.case_id, t1.no, t1.result_id
    having count(1) > 1;
 -- type dynCur is ref cursor;
 cursor curItem(no varchar2)  is 
 select *
        from table2 tb2,
             t_wf_instance                  tw,
             t_clm_case                     tc
       where tb2.claim_no = no;
       
 cursor curInd(no varchar2)   is 
     select tb1.*, tri.item_id

        from table3 t3

where t3.no=no;



begin


  for curca in curCase loop
    --flag:= datapatch_siu( curca.no);




    select tr.RESULT_ID
      into resultId
      from table4 tr, table5  tc
     where tr.case_id = tc.case_id
       and tc.claim_no = curca.claim_no;
    for cur in curItem(curca.claim_no) loop
      taskUpdateTime := to_char(cur.update_time, 'yyyymmddhh24miss');
      select to_char(ths.update_time, 'yyyy-mm-dd hh24:mi:ss')
        into dateOfFlag
        from t_icm_clm_mt_fraud_summary_his ths
       where ths.instance_id = cur.instance_id;
    
      select sum(decode(tb.loss_consequence,
                        '01',
                        nvl(tb.ext_field33, 0),
                        '02',
                        nvl(tb.ext_field33, 0),
                        0))
        into tpClaimAmount
        from t_clm_object tb
       where tb.case_id = cur.case_id
         and to_char(decode(tb.last_modify_date,
                            null,
                            tb.create_date,
                            tb.last_modify_date),
                     'yyyymmddhh24miss') <= taskUpdateTime;
    
      --dbms_output.put_line('tpClaimAmount:' || tpClaimAmount);
    
      -- dbms_output.put_line('taskUpdateTime:' || taskUpdateTime);
      instanceId := cur.instance_id;
      strTmp     := substr(cur.modify_history,
                           instr(cur.modify_history, 'Flag Score', -1, 1));
      --dbms_output.put_line('strTmp 1:' || strTmp);
      if instr(strTmp, chr(13)) > 0 then
        strTmp := substr(strTmp, 0, instr(strTmp, chr(13), 1, 1) - 1);
      end if;
    
      --dbms_output.put_line('strTmp 2:' || strTmp);
      flagScore  := substr(strTmp, instr(strTmp, ' ', -1, 1) + 1);
      flagCounts := 0;
      -- dbms_output.put_line('strTmp 3:' || strTmp);
      for curtmp in  curInd(curca.claim_no) loop
        itemId := curtmp.item_id;
      
        strSrc           := curtmp.modify_history;
        oneflagCountsTmp := 0;
        loop
        
          -- dbms_output.put_line('strSrc' || num || ':' || strSrc);
          if instr(strSrc, '<br>') = 0 or strSrc = '' or strSrc is null then
            tmp := strSrc;
          else
            tmp := substr(strSrc, 0, instr(strSrc, '<br>') - 1);
          end if;
        
          if tmp is not null then
            flagCheckTime := substr(tmp, 0, 19);
            flagCheckTime := substr(flagCheckTime, 7, 4) ||
                             substr(flagCheckTime, 4, 2) ||
                             substr(flagCheckTime, 0, 2) ||
                             replace(substr(flagCheckTime, 12), ':', '');
          
            if flagCheckTime < taskUpdateTime then
              strTmp := substr(strSrc,
                               instr(strSrc, ' ', 1, 3) + 1,
                               instr(strSrc, ' ', 1, 4) -
                               instr(strSrc, ' ', 1, 3) - 1);
              if strTmp = 'CHECKED' then
                --flagCounts := flagCounts + 1;
                oneflagCountsTmp := 1;
                flag             := 'Y';
              
              else
                flag := 'N';
                --if flagCounts>0 then
                --  flagCounts := flagCounts - 1;
                oneflagCountsTmp := 0;
                --end if;
              end if;
            else
              oneflagCountsTmp := 0;
              flag             := 'N';
            end if;
          else
            oneflagCountsTmp := 0;
            flag             := 'N';
          end if;
          num := num + 1;
          --dbms_output.put_line('flagCheckTime' || num || ':' ||
          -- flagCheckTime);
          exit when strSrc is null or strSrc = '' or instr(strSrc, '<br>') = 0;
        
          strSrc := substr(strSrc, instr(strSrc, '<br>') + 4);
          if strSrc is null or strSrc = '' then
            exit;
          end if;
        
        end loop;
        flagCounts := flagCounts + oneflagCountsTmp;
        select count(1)
          into extCount
          from t_clm_mt_fraud_result_item_his
         where INSTANCE_ID = instanceId
           and indicator_id = curtmp.indicator_id;
        --commit;
        curindicatorId := curtmp.indicator_id;
        if extCount = 0 then
          --dbms_output.put_line('insert extCount:' || extCount);
          execute immediate ' insert into table5
          (INSTANCE_ID, ITEM_ID, RESULT_ID, INDICATOR_ID, FLAG)
        values  (' || instanceId || ',' ||
                            itemId || ',' || resultId || ',' ||
                            curindicatorId || ', ''' || flag || ''')';
          --(instanceId, itemId, resultId, curtmp.indicator_id, flag);
        else

          --dbms_output.put_line('update extCount:' || extCount);

--update 语句如果是动态拼接的,需要拼接动态sql语句,如下。如果直接用sql语句,在where 条件后使用变量,会导致sql执行成功,而实际却不会更新,

--改成用动态sql拼接则可以更新成功。

          execute immediate ' update table5 trs  set trs.flag =''' || flag ||
                            ''' where trs.instance_id =' || instanceId || '
           and trs.indicator_id =' ||
                            curindicatorId;
          -- commit;
          -- dbms_output.put_line('num:' || num || ' instance_id:' || instanceId ||'  indicator_id:' ||curtmp.indicator_id || ' flag:' ||flag);
        
        end if;
      end loop;
      --dbms_output.put_line('flagCounts:' || flagCounts);
      --dbms_output.put_line('flagScore:' || flagScore);
      --dbms_output.put_line('tp_claim_amount:' || tpClaimAmount);
      --dbms_output.put_line('dateOfFlag:' || dateOfFlag);
      --select to_date('2003-10-17 21:15:37','yyyy-mm-dd hh24:mi:ss') into  from dual;

      --dbms_output.put_line('convert dateOfFlag:' || cur.update_time);


      update table6 ths
         set ths.flaged_counts      = to_number(flagCounts),
             ths.score              = to_number(flagScore),
             ths.tp_claim_amount    = to_number(tpClaimAmount),
             ths.date_flag_last_cal = cur.update_time
       where ths.instance_id = cur.instance_id;
    
    end loop;
  
  end loop;


end;
0 0