对其进行一些验证后更新列值

时间:2016-06-15 11:52:08

标签: sql oracle plsql oracle12c

我有一个场景,我需要更新列的值或者说要追加!

下面是测试表和数据

create table test (rsrc_nm varchar2(50), parm varchar2(400)); 

insert into test values ('HLRCamelProfileBasic','resource_code:7|resource_type:HLR_Camel_Profile|Priority:1|');
insert into test values ('HSSUSERProfileBasic','resource_code:3|resource_type:HSS_User_Profile|Priority:1|'); 
insert into test values ('HSSUSERProfileBasic','resource_code:3|resource_type:HSS_User_Profile|Priority:2|');
insert into test values ('HSSUSERProfileBasic','resource_code:3|resource_type:HSS_User_Profile|Priority:1|');
insert into test values ('HLRBaseProfileBasic','resource_code:1|resource_type:HLR_Base_Profile|Priority:2|');
insert into test values ('HLRBaseProfileBasic','resource_code:1|resource_type:HLR_Base_Profile|Priority:3|');

这里我们有2列临时表,其中给出了rsrc_nm和parm我需要做的是我需要更新或附加parm coulmn的rsrc_nm列,其中最后一个值即优先级从1变为2相同的rsrc_nm。

例如

rsrc_ nm -- HSSUSERProfileBasic      parm -- resource_code:3|resource_type:HSS_User_Profile|Priority:1|                     
rsrc_ nm -- HSSUSERProfileBasic      parm -- resource_code:3|resource_type:HSS_User_Profile|Priority:2|

这里我们有相同的parm优先级,但值不同,我需要将这些值插入到另一个表中,我需要将所有PIPE值分开,所以当我将其插入表中时,它会给出我唯一约束的错误,因为表格有3列

RSRC_NM                                   PARM                  VAL
-------------------------------        --------------            ----------------
HSSUSERProfileBasic            Priority                  1
HLRCamelProfileBasic            Priority                    1
HLRBaseProfileBasic              Priority                    2

在这个表上,我们在前两列上有主键,强制执行唯一约束,所以我不能为parm“Priority”插入“HLRCamelProfileBasic”rsrc_nm,因为它的唯一性失败。

所以我找到了一个解决方案来解决这个问题,如果我可以将rsrc_nm HLRCamelProfileBasic附加或更新为“HLRCamelProfileBasic_1”以获取“Priority:1”而将HLRCamelProfileBasic_2更新为“Priority:2”,依此类推,以便在Staging表中使用所有RSRC_NM。

我的解决方案 -

declare

  cursor c1 is select * from RSRC_SVC_MAPPING where parm_entry like '%Priorit%'; 

  parm_entry_length number;
  l_value     varchar2(100);
  parm_name   varchar2(100);
  parm_val    varchar2(100); 
  l_c1       c1%rowtype;
  l_cnt      number := 0;
  l_rsrc_nm   varchar2(100); 
begin

  open c1;
  loop
  fetch c1 into l_c1;

   parm_entry_length := length(l_c1.Parm_Entry) -   length(replace(l_c1.Parm_Entry,'|',''));  
     for i in 1 .. parm_entry_length loop 
         select regexp_substr(l_c1.Parm_Entry,'[^|]+',1,i) into l_value from dual;   
        select regexp_substr (l_value, '[^:]+', 1, 1) into parm_name from dual;    
         select regexp_substr (l_value, '[^:]+', 1, 2) into parm_val from dual; 
    -- dbms_output.put_line(l_value||' '||parm_name||' '||parm_val||' '||l_c1.resourcename);

   for r in ( select count(*) cnt, resourcename  
     -- into l_cnt , l_rsrc_nm
      from (select count(*), resourcename, parm_entry from RSRC_SVC_MAPPING      where parm_entry like '%Priorit%' group by resourcename, parm_entry) group by resourcename)
      loop
    if r.cnt > 1 
     then
     dbms_output.put_line(l_value||' '||parm_name||' '||parm_val||' '||l_c1.resourcename);
       update RSRC_SVC_MAPPING
          set resourcename = r.resourcename||'_'||l_cnt
       where resourcename  = r.resourcename;
       l_cnt := l_cnt +1;
     end if;
     end loop;
   end loop;
   exit when c1%notfound;
   end loop;

 exception 
   when others then 
   dbms_output.put_line('ERROR OCCURED '||SQLCODE||' '||sqlerrm);
        dbms_output.put_line(dbms_utility.format_error_backtrace());  
 end;  

UPDATE之后需要的数据就像

HSSUSERProfileBasic_1                    resource_code:3|resource_type:HSS_User_Profile|Priority:1|
HSSUSERProfileBasic_2                        resource_code:3|resource_type:HSS_User_Profile|Priority:2|
HLRBaseProfileBasic_2                        resource_code:1|resource_type:HLR_Base_Profile|Priority:2|
HLRBaseProfileBasic_3                       resource_code:1|resource_type:HLR_Base_Profile|Priority:3|

这里我们更改了值rsrc_nm列并将其附加了优先级值,即1和2

目标表就像

create table target (rsrc_nm varchar2(100), parm varchar2(50), val varchar2(50) constraint tgt_pk primary key (rsrrc_nm, parm));

MY DB is -- Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production

0 个答案:

没有答案