【问题标题】:Add invalid remarks for each invalid records inserted into the invalid table in oracle为oracle中插入无效表的每条无效记录添加无效备注
【发布时间】:2020-08-19 20:33:52
【问题描述】:

我有一个cursor,它根据几个标准将有效数据插入有效表,将无效数据插入无效表。下面是我的光标逻辑。

create or replace PROCEDURE FIP_VAL_INV_DATA AS 
l_state_name r4g_lb.mantainenceboundary_evw.jiostatename%type;
l_maint_zone_code r4g_lb.mantainenceboundary_evw.maintenancezonecode%type;
l_maint_zone_name r4g_lb.mantainenceboundary_evw.maintenancezonename%type;
l_state_code r4g_lb.mantainenceboundary_evw.jiostatecode%type;

begin

  for cur_r in (select rj_span_id, 
                       rj_maintenance_zone_name,
                       rj_maintenance_zone_code,
                       rj_state_name,
                       rj_network_category,
                       rj_network_type,
                       rj_construction_methodology,
                       inventory_status_code,
                       rj_route_name,
                       rj_intracity_link_id,
                       calculated_length 
                      from app_fttx.transmedia@sat 
                      where --rownum < 100 and
                      jumper_flag is null
               )               
  loop 
  
  select max(jiostatename)
      into l_state_name
      from r4g_lb.mantainenceboundary_evw
      where jiostatename = cur_r.rj_state_name
      and rownum = 1;
      
--dbms_output.put_line('test');

      select max(maintenancezonecode), max(maintenancezonename)
      into l_maint_zone_code, l_maint_zone_name
      from r4g_lb.mantainenceboundary_evw
      where maintenancezonecode = cur_r.rj_maintenance_zone_code
      and maintenancezonename = cur_r.rj_maintenance_zone_name
      and rownum = 1;
  
 
   
      if length(cur_r.rj_span_id) =  '21'
          and cur_r.inventory_status_code = 'IPL'
          and regexp_like(cur_r.rj_span_id, 'SP(N|Q|R|S).*_(BU|MP)$')
          and NVL(INSTR(cur_r.RJ_INTRACITY_LINK_ID, '_'), 1) > 0 
          and cur_r.rj_maintenance_zone_code = l_maint_zone_code
          and cur_r.rj_maintenance_zone_name = l_maint_zone_name
          and cur_r.rj_state_name = l_state_name  
          
          
    then 
      begin      
       INSERT INTO tbl_fiber_valid_trans_data 
         (span_id, maintenance_zone_name, maintenance_zone_code, r4g_state_name, inventory_status_code, network_category, network_type, construction_methodology, route_name,intracity_link_id, calculated_length, last_updated_by)
          values
         (cur_r.rj_span_id, cur_r.rj_maintenance_zone_name, cur_r.rj_maintenance_zone_code, cur_r.rj_state_name, cur_r.inventory_status_code, cur_r.rj_network_category, cur_r.rj_network_type, cur_r.rj_construction_methodology, cur_r.rj_route_name, cur_r.rj_intracity_link_id, cur_r.calculated_length, 'Test');
      end;      
   
    elsif LENGTH(cur_r.rj_intracity_link_id) > 8 
              AND LENGTH(cur_r.rj_intracity_link_id) < 21
              and cur_r.inventory_status_code = 'IPL'    
             and cur_r.rj_maintenance_zone_code = l_maint_zone_code
             and cur_r.rj_maintenance_zone_name = l_maint_zone_name
            and cur_r.rj_state_name = l_state_name  
   then
    begin
     INSERT INTO tbl_fiber_valid_trans_data 
         (span_id, maintenance_zone_name, maintenance_zone_code, r4g_state_name, inventory_status_code, network_category, network_type, construction_methodology, route_name,intracity_link_id, calculated_length, last_updated_by)
          values
         (cur_r.rj_span_id, cur_r.rj_maintenance_zone_name, cur_r.rj_maintenance_zone_code, cur_r.rj_state_name, cur_r.inventory_status_code, cur_r.rj_network_category, cur_r.rj_network_type, cur_r.rj_construction_methodology, cur_r.rj_route_name, cur_r.rj_intracity_link_id, cur_r.calculated_length, 'Test');
      
    end;
    else begin
        INSERT INTO TBL_FIBER_INVALID_TRANS_DATA 
          (span_id, maintenance_zone_name, maintenance_zone_code, r4g_state_name, inventory_status_code, network_category, network_type, construction_methodology, route_name,intracity_link_id, calculated_length, last_updated_by, remarks)
          values
         (cur_r.rj_span_id, cur_r.rj_maintenance_zone_name, cur_r.rj_maintenance_zone_code, cur_r.rj_state_name, cur_r.inventory_status_code, cur_r.rj_network_category, cur_r.rj_network_type, cur_r.rj_construction_methodology, cur_r.rj_route_name, cur_r.rj_intracity_link_id, cur_r.calculated_length, 'Test', dynamic remarks based upon the error);
         end;        
         
         
    end if;       
  end loop; 
  
END FIP_VAL_INV_DATA;

所以现在我想要的是,如果一行无效并被插入到无效表中,即TBL_FIBER_INVALID_TRANS_DATA。我也想插入备注,记录无效的原因。

为了增加原因,我有上面提到的几个条件,

if length(cur_r.rj_span_id) = '21'假设长度不等于21。我们可以添加备注为The length is not equal to 21 characters

这样我们可以在无效表的备注栏中动态添加几个备注。

请提出如何实现它。

更新

【问题讨论】:

    标签: oracle stored-procedures cursor


    【解决方案1】:

    您可以选择多个选项。一种是创建额外的表来记录您发现的所有错误。它有两列:IDERR_CODE。然后,您必须搜索源数据集并将行插入该表,例如

    ERR_CODE = 1 = length not equal to 21
    

    然后:

    insert into errors (id, err_code)
      select t.id, 1
      from source_table t
      where length(some_column) <> 21;
    

    它允许您为同一个 ID 存储多个错误,并且它会被规范化


    另一种选择是更改TBL_FIBER_INVALID_TRANS_DATA 并添加errors varchar2(100);您仍然需要找出问题所在并

    • 在每个错误描述中为每个 ID 插入一行(因此,如果有很多错误,则在每个 ID 中插入多行)
    • 每个 ID 只插入一行并将错误代码连接到新添加的列中(因此如果 ID 包含错误 1、8 和 12,它将包含例如 1/8/12)。如果只有几个错误,这没关系,因此您可以通过查看它来了解正在发生的事情,但是 - 如果您捕获的错误更多,它会变得难以理解,它没有标准化并且您会遇到其他问题如果您想展示在一个 ID 上发现的所有错误

    如果我是你,我可能会选择选项 #1 和附加表。然后,通过在同一 ID 上加入 TBL_FIBER_INVALID_TRANS_DATAERRORS,可以轻松地为最终用户(或您自己)创建报告。

    【讨论】:

    【解决方案2】:

    为每个游标记录定义一个变量l_invalid_reason。单独执行每个验证,如果验证失败,将相应的消息写入 l_invalid_reason。

    然后,如果l_invalid_reasonnull,则将其插入“有效”表中。否则,插入“无效”表,将l_invalid_reason 传递给remarks 列。这是使用 cmets 执行此操作的代码的精简版本。

    create or replace PROCEDURE FIP_VAL_INV_DATA AS 
    
    begin
      -- Open your cursor loop
      for cur_r in (select ... ) loop
    
      declare
        l_invalid_reason VARCHAR2(100);
      begin
        -- Do various select into commands to get extra data needed (e.g. l_state_name, l_maint_zone_code, and l_maint_zone_name
        select max(jiostatename) into l_state_name...
        ...
        ...
    
    
        -- Do each validation individually
        -- Notice you are checking for INVALID conditions, not VALID ones, so the
        -- logic is reversed from your posting and you may need to account for possible nulls, 
        -- as in some of the examples below.
        if length(cur_r.rj_span_id) != 21 then
          l_invalid_reason := 'Length is not 21 characters exactly';
        else if nvl(cur_r_inventory_status_code,'XXX') != 'IPL' then
          l_invalid_reason := 'Inventory status code is not IPL';
        else if not regexp_like(cur_r.rj_span_id, 'SP(N|Q|R|S).*_(BU|MP)$') then
          l_invalid_reason := 'Span ID does not match accepted format';
        else if .... (additional validations) ...
    
        end if;
    
        -- Check to see whether there is an invalid reason
        if l_invalid_reason is null then
          -- insert into good table
        else
          -- insert into invalid table
            INSERT INTO TBL_FIBER_INVALID_TRANS_DATA 
              (span_id, 
               maintenance_zone_name, 
               maintenance_zone_code, 
               ...
               remarks)
              values
             (cur_r.rj_span_id, 
              cur_r.rj_maintenance_zone_name, 
              cur_r.rj_maintenance_zone_code, 
              ...
              l_invalid_reason);  -- put the PL/SQL variable holding the invalid reason into the INSERT statement
        end if;        
      end;       
      end loop; 
    
    END FIP_VAL_INV_DATA;
    

    【讨论】:

      猜你喜欢
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多